• Python gui


    源代码:
    from tkinter import *
    a = Tk()
    a.title('林计算')
    a.geometry('260x455')

    def numberinput(number):
    first_num=en.get()
    en.delete(0,END)
    print(number,type(number))
    en.insert(0,str(first_num)+str(number))

    def clear():
    en.delete(0,END)
    an.delete(0,END)

    def plus():
    global o
    global first_mum
    global second_mum
    first_mum=en.get()
    en.delete(0,END)
    o='add'

    def minus():
    global o
    global first_mum
    first_mum=en.get()

    en.delete(0,END)
    o='min'
    

    def multi():
    global o
    global first_mum
    first_mum=en.get()

    en.delete(0,END)
    o='mul'
    

    def times():
    global o
    global first_mum
    first_mum=en.get()
    en.delete(0,END)
    o='div'

    def equal():
    second_mum=en.get()
    en.delete(0,END)

    if o=='add':
        en.insert(0,first_mum+'+'+second_mum)
        an.insert(0,int(first_mum)+int(second_mum))
    if o=='min':
        en.insert(0,first_mum+'-'+second_mum)
        an.insert(0,int(first_mum)-int(second_mum))
    if o=='mul':
        en.insert(0,first_mum+'*'+second_mum)
        an.insert(0,int(first_mum)*int(second_mum))
    if o=='div':
        en.insert(0,first_mum+'/'+second_mum)
        an.insert(0,int(first_mum)/int(second_mum))
    

    bu1=Button(a,text=1,width=2,font=('Arial',31),command=lambda:numberinput(1))
    bu2=Button(a,text=2,width=2,font=('Arial',31),command=lambda:numberinput(2))
    bu3=Button(a,text=3,width=2,font=('Arial',31),command=lambda:numberinput(3))
    bu4=Button(a,text=4,width=2,font=('Arial',31),command=lambda:numberinput(4))
    bu5=Button(a,text=5,width=2,font=('Arial',31),command=lambda:numberinput(5))
    bu6=Button(a,text=6,width=2,font=('Arial',31),command=lambda:numberinput(6))
    bu7=Button(a,text=7,width=2,font=('Arial',31),command=lambda:numberinput(7))
    bu8=Button(a,text=8,width=2,font=('Arial',31),command=lambda:numberinput(8))
    bu9=Button(a,text=9,width=2,font=('Arial',31),command=lambda:numberinput(9))
    bu0=Button(a,text=0,width=2,font=('Arial',31),command=lambda:numberinput(0))
    bu_eq=Button(a,text='=',width=2,font=('Arial',31),command=lambda:equal())
    bu_clear=Button(a,text='C',width=2,font=('Arial',31),command=lambda:clear())
    bu_add=Button(a,text='+',width=2,font=('Arial',31),command=lambda:plus())
    bu_min=Button(a,text='-',width=2,font=('Arial',31),command=lambda:minus())
    bu_mul=Button(a,text='*',width=2,font=('Arial',31),command=lambda:multi())
    bu_div=Button(a,text='/',width=2,font=('Arial',31),command=lambda:times())
    bu1.place(x=15,y=100)
    bu2.place(x=75,y=100)
    bu3.place(x=135,y=100)
    bu4.place(x=15,y=185)
    bu5.place(x=75,y=185)
    bu6.place(x=135,y=185)
    bu7.place(x=15,y=270)
    bu8.place(x=75,y=270)
    bu9.place(x=135,y=270)
    bu0.place(x=75,y=355)
    bu_eq.place(x=135,y=355)
    bu_clear.place(x=15,y=355)
    bu_add.place(x=195,y=100)
    bu_min.place(x=195,y=185)
    bu_mul.place(x=195,y=270)
    bu_div.place(x=195,y=355)

    an=Entry(a,width=10,font=('Arial',31))
    an.place(x=10,y=45)
    en=Entry(a,width=10,font=('Arial',31))
    en.place(x=10,y=00)

    a.mainloop()

    写代码的感受:当要布局的窗口比较多的时候,用place不尽代码很长,不易书写,而且定位的像素坐标试了好久,非常麻烦,如果选用pack或grid来布局,会简单很多。我写的思路比较简单,但书写操作起来麻烦;如果用grid来写,可能思路上困难一些,但是敲代码的过程肯定会简单很多。在网上看见了篇代码:
    from tkinter import *
    reset=True
    def buttonCallBack(event):
    global label
    global reset
    num=event.widget['text']
    if num'C':
    label['text']="0"
    return
    if num in "=":
    label['text']=str(eval(label['text']))
    reset=True
    return
    s=label['text']
    if s
    '0' or reset==True:
    s=""
    reset=False
    label['text']=s+num

    主窗口

    root=Tk()
    root.wm_title("林计算")

    显示栏1

    label=Label(root,text="0",background="yellow",anchor="e")
    label['width']=55
    label['height']=6
    label.grid(row=1,columnspan=4,sticky=W)

    按钮

    showText="789/456123-0.C+"
    for i in range(4):
    for j in range(4):
    b=Button(root,text=showText[i
    4+j],width=7)
    b.grid(row=i+2,column=j)
    b.bind("",buttonCallBack)
    showText="()"
    for i in range(2):
    b=Button(root,text=showText[i],width=7)
    b.grid(row=6,column=2+i)
    b.bind("",buttonCallBack)
    b=Button(root,text="=")
    b.grid(row=6,columnspan=2,sticky="we")
    b.bind("",buttonCallBack)
    root.mainloop()

    书写简单,而且功能也比我强大。感觉自己写代码的路还有很多长要走。
    附件1:我的代码执行
    附件2:网络上代码执行
    参考:https://www.cnblogs.com/pinpin/p/10052193.html

  • 相关阅读:
    C语言不进行类型检查 和函数能够不进行前向声明
    EventBus 《二》 Android EventBus的简单使用
    android开发之SnackBar的使用
    iOS语音播放之切换听筒和扬声器
    使用NSURLConnection的网络请求与封装
    Mina Basics 02-基础
    Mina Basics 02-基础
    jquery移除事件,绑定事件,触发事件
    jquery移除事件,绑定事件,触发事件
    jquery移除事件,绑定事件,触发事件
  • 原文地址:https://www.cnblogs.com/wxl2761407387/p/14199173.html
Copyright © 2020-2023  润新知