Variable category learning of 06 Tkinter learning

Keywords: Python Lambda calculator

There are four categories of variables

IntVar(): integer variable, default is 0.

DoubleVar(): floating point variable, default is 0.0

StringVar(): string variable, null by default

Boolean var(): boolean variable, True is 1, False is 0.

41 set() and get() usage:

from tkinter import *
win=Tk()
def view():
    global Message
    if Message==False:
        Message=True
        x.set("tkinter")
    else:
        Message=False
        x.set("")
Message=False
x=StringVar()
label=Label(win,textvariable=x,fg="blue",bg="yellow",width=25,height=2)
label.pack()
button=Button(win,text="click",command=view)
button.pack()
win.mainloop()

42 - trace() uses mode W, and the window synchronously displays the input content:

from tkinter import *
def callback(*args):
    print(x.get())

win=Tk()
x=StringVar()
entry=Entry(win,textvariable=x)
entry.pack(padx=5,pady=5)
x.trace("w",callback)
win.mainloop()

The first parameter of x.trace("w",callback) is the mode. W means that when a write is executed, the callback function will be executed automatically, or the function name can be obtained by yourself. This is called change tracking. When the first parameter is r, it means reading is performed, which is called read tracing.

43 - Implementation of simple calculator:

from tkinter import *
win=Tk()
def calculate():
    result=eval(equ.get())
    equ.set(equ.get()+"=\n"+str(result))
def show(buttonString):
    content=equ.get()
    if content=="0":
        content=""
    equ.set(content+buttonString)
def backspace():
    equ.set(str(equ.get()[:-1]))
def clear():
    equ.set("0")
equ=StringVar()
equ.set("0")
label=Label(win,width=25,height=2,relief="raised",anchor=SE,textvariable=equ)
label.grid(row=0,column=0,columnspan=4,padx=5,pady=5)
clearbutton=Button(win,text="C",width=5,fg="blue",command=clear)
clearbutton.grid(row=1,column=0)
Button(win,text="DEL",width=5,command=backspace).grid(row=1,column=1)
Button(win,text="%",width=5,command=lambda:show("%")).grid(row=1,column=2)
Button(win,text="/",width=5,command=lambda:show("/")).grid(row=1,column=3)
Button(win,text="7",width=5,command=lambda:show("7")).grid(row=2,column=0)
Button(win,text="8",width=5,command=lambda:show("8")).grid(row=2,column=1)
Button(win,text="9",width=5,command=lambda:show("9")).grid(row=2,column=2)
Button(win,text="*",width=5,command=lambda:show("*")).grid(row=2,column=3)
Button(win,text="4",width=5,command=lambda:show("4")).grid(row=3,column=0)
Button(win,text="5",width=5,command=lambda:show("5")).grid(row=3,column=1)
Button(win,text="6",width=5,command=lambda:show("6")).grid(row=3,column=2)
Button(win,text="-",width=5,command=lambda:show("-")).grid(row=3,column=3)
Button(win,text="1",width=5,command=lambda:show("1")).grid(row=4,column=0)
Button(win,text="2",width=5,command=lambda:show("2")).grid(row=4,column=1)
Button(win,text="3",width=5,command=lambda:show("3")).grid(row=4,column=2)
Button(win,text="+",width=5,command=lambda:show("+")).grid(row=4,column=3)
Button(win,text="0",width=12,command=lambda:show("0")).grid(row=5,column=0,columnspan=2)
Button(win,text=".",width=5,command=lambda:show(".")).grid(row=5,column=2)
Button(win,text="=",width=5,bg="yellow",command=lambda:calculate()).grid(row=5,column=3)
win.mainloop()

The purpose of using lambda in the program is to simplify the design, because the number button uses the same function as the arithmetic expression button.

Posted by Clerma on Wed, 27 Nov 2019 07:36:43 -0800