IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Python知识库 -> 200行代码完成Python寒假作业 -> 正文阅读

[Python知识库]200行代码完成Python寒假作业


不多说,上代码:

from tkinter import *  
from tkinter import messagebox 
from playsound import *  
from threading import *  
from linecache import *
from random import *

CreateButton = locals()
putcur=0
code="""
from math import *
global num
num="""

def curset(n,var2):
    if n>=41:var2.set("文本已占满!请清空!")
    else:var2.set("当前操作位数:"+str(n))
    
def puttext(var,text,TF_append):
    global putcur
    Restr = ""
    line = list(var.get())
    long = len(line)
    if TF_append == 0: line.insert(putcur+long,text)
    else: del line[putcur+long-1]
    Restr = ''.join(line)
    return Restr
    
def ErrMe(title,text):
    messagebox.showerror(title,text,type="ok")

def run(var):
    t = "错误"
    try:
        exec(code+var.get())
        return num
    except EOFError:
        ErrMe("文件读取错误!","你是不是把里面的文件删了?")
        return t
    except SyntaxError:
        ErrMe("识别错误","你是不是漏加或多加了一个括号?\n还是写了个令程序迷惑的表达式?")
        return t
    except TypeError:
        ErrMe("格式错误","少加或多加了几个参数!\n或是给错了不符合要求的值!")
        return t    
    except ZeroDivisionError:
        return '无限大'
    except NameError:
        ErrMe("变量错误","这里不能定义变量!")
        return t
    except ValueError:
        ErrMe("赋值错误","认真看一下'帮助'中的'查看拓展'再重新赋值!")
        return t

def opensound():
    playsound("resoures\\Windows_open.wav")

def in_and_out(n, var,var2,root,telab):
    global putcur
    if n == 1:  
        var.set("")
        putcur = 0
        curset(len(var.get())+putcur,var2)
    elif n == 23: 
        if var.get() == "":
            var.set("")
        else:
            var.set(run(var))
            curset(len(var.get())+putcur,var2)
    elif n==27:
        try:
            text = getline('resoures\\box.txt', n).strip('\n')
            var.set(puttext(var,text,1))
            curset(len(var.get())+putcur,var2)
        except IndexError:
            putcur = 0
            var2.set("当前操作位数:"+str(len(var.get())))
    elif n==28:
        CreateHelp(root,telab)
    elif n==29:
        putcur = putcur - 1
        curset(len(var.get())+putcur,var2)
    elif n==30:
        putcur = putcur + 1
        curset(len(var.get())+putcur,var2)
    else:  
        text = getline('resoures\\box.txt', n).strip('\n')
        var.set(puttext(var,text,0))
        curset(len(var.get())+putcur,var2)

def xput(wrv,var,var2):
    global putcur
    var.set(puttext(var,wrv.get(),0))
    wrv.set("")
    curset(len(var.get())+putcur,var2)
   
def main(root):
    var = StringVar()
    var2 = StringVar()
    wrv = StringVar()
    var.set("") 
    wrv.set("")

    root.title("我的计算器")  
    root.iconbitmap("resoures\\dkss.ico")  
    root.geometry("640x386")
    root.geometry("+320+160")
    root.resizable(0, 0)  
    root.config(cursor = "@AppStarting.ani")
   
    photo = PhotoImage(file="resoures\\bg.gif")  
    bg = Canvas(root, width=640, height=386, highlightthickness=0) 
    bg.create_image(320, 193, image=photo) 
    bg.pack()  
    telab = Label(root, textvariable=var, highlightthickness=2, anchor=NW,cursor="@IBeam.ani",relief=SUNKEN)
    telab.pack()
    curlab = Label(root,textvariable=var2,anchor=NW,cursor="@undertext1.ani",relief=RIDGE)
    curlab.pack()
    stick = Entry(root,textvariable=wrv,cursor="@entry1.ani")
    stick.pack()
    click = Button(root,text="粘贴",cursor="@click.ani",fg="DarkBlue",command=lambda:xput(wrv,var,var2))
    click.pack()

    CreateButton['cw' + str(1)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 1).strip('\n'), command= lambda: in_and_out(1, var,var2,root,telab))
    CreateButton['cw' + str(2)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 2).strip('\n'), command= lambda: in_and_out(2, var,var2,root,telab))
    CreateButton['cw' + str(3)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 3).strip('\n'), command= lambda: in_and_out(3, var,var2,root,telab))
    CreateButton['cw' + str(4)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 4).strip('\n'), command= lambda: in_and_out(4, var,var2,root,telab))
    CreateButton['cw' + str(5)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 5).strip('\n'), command= lambda: in_and_out(5, var,var2,root,telab))
    CreateButton['cw' + str(6)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 6).strip('\n'), command= lambda: in_and_out(6, var,var2,root,telab))
    CreateButton['cw' + str(7)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 7).strip('\n'), command= lambda: in_and_out(7, var,var2,root,telab))
    CreateButton['cw' + str(8)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 8).strip('\n'), command= lambda: in_and_out(8, var,var2,root,telab))
    CreateButton['cw' + str(9)] =  Button(root, width=7, height=2, text=getline('resoures\\text.txt', 9).strip('\n'), command= lambda: in_and_out(9, var,var2,root,telab))
    CreateButton['cw' + str(10)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 10).strip('\n'),command=lambda: in_and_out(10, var,var2,root,telab))
    CreateButton['cw' + str(11)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 11).strip('\n'),command=lambda: in_and_out(11, var,var2,root,telab))
    CreateButton['cw' + str(12)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 12).strip('\n'),command=lambda: in_and_out(12, var,var2,root,telab))
    CreateButton['cw' + str(13)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 13).strip('\n'),command=lambda: in_and_out(13, var,var2,root,telab))
    CreateButton['cw' + str(14)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 14).strip('\n'),command=lambda: in_and_out(14, var,var2,root,telab))
    CreateButton['cw' + str(15)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 15).strip('\n'),command=lambda: in_and_out(15, var,var2,root,telab))
    CreateButton['cw' + str(16)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 16).strip('\n'),command=lambda: in_and_out(16, var,var2,root,telab))
    CreateButton['cw' + str(17)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 17).strip('\n'),command=lambda: in_and_out(17, var,var2,root,telab))
    CreateButton['cw' + str(18)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 18).strip('\n'),command=lambda: in_and_out(18, var,var2,root,telab))
    CreateButton['cw' + str(19)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 19).strip('\n'),command=lambda: in_and_out(19, var,var2,root,telab))
    CreateButton['cw' + str(20)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 20).strip('\n'),command=lambda: in_and_out(20, var,var2,root,telab))
    CreateButton['cw' + str(21)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 21).strip('\n'),command=lambda: in_and_out(21, var,var2,root,telab))
    CreateButton['cw' + str(22)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 22).strip('\n'),command=lambda: in_and_out(22, var,var2,root,telab))
    CreateButton['cw' + str(23)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 23).strip('\n'),command=lambda: in_and_out(23, var,var2,root,telab))
    CreateButton['cw' + str(24)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 24).strip('\n'),command=lambda: in_and_out(24, var,var2,root,telab))
    CreateButton['cw' + str(25)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 25).strip('\n'),command=lambda: in_and_out(25, var,var2,root,telab))
    CreateButton['cw' + str(26)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 26).strip('\n'),command=lambda: in_and_out(26, var,var2,root,telab))
    CreateButton['cw' + str(27)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 27).strip('\n'),command=lambda: in_and_out(27, var,var2,root,telab))
    CreateButton['cw' + str(28)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 28).strip('\n'),command=lambda: in_and_out(28, var,var2,root,telab))
    CreateButton['cw' + str(29)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 29).strip('\n'),command=lambda: in_and_out(29, var,var2,root,telab))
    CreateButton['cw' + str(30)] = Button(root, width=7, height=2, text=getline('resoures\\text.txt', 30).strip('\n'),command=lambda: in_and_out(30, var,var2,root,telab))

    for i in range(1,31):
       if i==28:CreateButton['cw' + str(i)].config(cursor="@Help.ani")
       else:CreateButton['cw' + str(i)].config(cursor="@Arrow.ani")
       CreateButton['cw'+str(i)].config(relief=GROOVE)
       CreateButton['cw' + str(i)].pack() 
    bg.create_window(462, 25, width=294, window=telab)  
    for i in range(1,31):
       bg.create_window(344 + ((i - 1) % 5) * 59, 75 + ((i - 1) // 5) * 47,window=CreateButton['cw' + str(i)])
    bg.create_window(550,360 ,width=120, window=curlab)
    bg.create_window(375,360 ,width=120, window=stick)
    bg.create_window(462,360 ,height=24, window=click)
    root.mainloop()

def change(childvar):
    n = randint(1,57)
    if n%2==1:childvar.set(getline("resoures\\tips.txt",n)+getline("resoures\\tips.txt",n+1).strip('\n'))
    else:childvar.set(getline("resoures\\tips.txt",n-1)+getline("resoures\\tips.txt",n).strip('\n'))

def onclose(root,childroot,telab):
    global CreateButton
    childroot.destroy()
    root.config(cursor = "@AppStarting.ani")
    root.title("我的计算器")
    telab.config(cursor="@IBeam.ani")
    for i in range(1,31):
        if i==28:CreateButton['cw'+str(i)].config(cursor="@Help.ani")
        else:CreateButton['cw'+str(i)].config(cursor="@Arrow.ani")

def anotherclose(childroot,ccroot,l1,l2,b1,b2):
    ccroot.destroy()
    childroot.config(cursor = "@wait.ani")
    childroot.title("帮助")
    l2.config(cursor="@undertext2.ani")
    b1.config(cursor="@b1.ani")
    b2.config(cursor="@b2.ani")

def CreateHelp(root,telab):
    global CreateButton
    childvar = StringVar()
    childvar.set(getline("resoures\\tips.txt",1)+getline("resoures\\tips.txt",2).strip('\n'))
    childroot = Toplevel()
    childroot.geometry("545x600")
    childroot.geometry("+80+30")
    childroot.title("帮助")
    childroot.resizable(0,0)
    childroot.iconbitmap("resoures\\childroot.ico")
    childroot.config(cursor="@wait.ani")
    root.config(cursor = "@NO.ani")
    root.title("此窗口已被禁用")
    telab.config(cursor="@NO2.ani")
    for i in range(1,31):
        if i==28:CreateButton['cw'+str(i)].config(cursor="@wait.ani")
        else:CreateButton['cw'+str(i)].config(cursor="@hand.ani")
    l1 = Label(childroot,image=photoc)
    l1.pack()
    l2 = Label(childroot,height=20,width=50,textvariable=childvar,relief=GROOVE,cursor="@undertext2.ani",fg="Purple")
    l2.pack(side=LEFT)
    b1 = Button(childroot,width=12,height=2,text="刷新",cursor="@b1.ani",fg="OrangeRed",command=lambda:change(childvar))
    b1.pack(side=LEFT)
    b2 = Button(childroot,width=12,height=2,text="查看拓展",cursor="@b2.ani",fg="DeepSkyblue",command=lambda:loadtk(childroot,l1,l2,b1,b2))
    b2.pack(side=RIGHT)
    childroot.grab_set()
    childroot.protocol('WM_DELETE_WINDOW',lambda:onclose(root,childroot,telab))

def loadtk(childroot,l1,l2,b1,b2):
    childroot.title("此窗口已被禁用")
    ccroot = Toplevel()
    ccroot.geometry("599x500")
    ccroot.title("拓展库大全")
    ccroot.iconbitmap("resoures\\ccroot.ico")
    ccroot.config(cursor="@ccroot.ani")
    childroot.config(cursor="@crstop.ani")
    l2.config(cursor="@t1wait.ani")
    Label(ccroot,image=photoj,relief=RIDGE).pack(side=LEFT)
    Label(ccroot,image=photox,relief=RIDGE,cursor="@VX.ani").pack(side=TOP)
    Label(ccroot,image=photodl,relief=RIDGE,cursor="@getmy.ani").pack()
    Label(ccroot,text="制作不易,赏点钱吧!\n光标资源来自大佬  '猫知道'  \n致此鸣谢",fg='blue',relief=SUNKEN,width=99,height=30).pack()
    b1.config(cursor="@Notallow.ani")
    b2.config(cursor="@ccroot.ani")
    ccroot.grab_set()
    ccroot.protocol('WM_DELETE_WINDOW',lambda:anotherclose(childroot,ccroot,l1,l2,b1,b2))

if __name__ == "__main__":
    thread = Thread(target=opensound)
    thread.start()
    root = Tk()
    photoc = PhotoImage(file="resoures\\Help.gif")
    photoj = PhotoImage(file="resoures\\load.gif")
    photox = PhotoImage(file="resoures\\load1.gif")
    photodl = PhotoImage(file="resoures\\QAQ.gif")
    main(root) 
    thread.join()  
    playsound("resoures\\Windows_close.wav") 

在这里插入图片描述

点我传送 密码:dgaf

  Python知识库 最新文章
Python中String模块
【Python】 14-CVS文件操作
python的panda库读写文件
使用Nordic的nrf52840实现蓝牙DFU过程
【Python学习记录】numpy数组用法整理
Python学习笔记
python字符串和列表
python如何从txt文件中解析出有效的数据
Python编程从入门到实践自学/3.1-3.2
python变量
上一篇文章      下一篇文章      查看所有文章
加:2022-02-01 20:33:48  更:2022-02-01 20:35:09 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/16 1:48:21-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码