Tkinter计算器不工作:StringVar()不在标签文本中更新

时间:2017-06-30 17:09:43

标签: python tkinter

我试图让这项工作成功,但它无法正常工作。

我哪里错了?标签文字显示... None21None31None41None51 ...这样的系列。

我尝试将变量与IntVar()连接起来。它永远不会从0改变值。

这是代码。这是我第一次在Tkinter编写代码。请帮忙。

from Tkinter import *
import ttk
x,y,oper=None,None,None

w = ""

def var_val(one):
    global x, y, w,myresult
    if not x:
        x = one
        w = w + str(x)
        myresult.set(w)
    else:
        y = one
        w = w + str(x) + str(oper) + str(y)
        myresult.set(w)

 def oper_val(two):
     global oper
     oper = two

def get_result():
     global oper, x, y, w, myresult
    if oper == "+":
        z = x + y
        w = str(z)
        myresult.set(w)
    elif oper == "-":
        z = x - y
        w = str(z)
        myresult.set(w)
    elif oper == "*":
        z = x * y
        w = str(z)
        myresult.set(w)
    elif oper == "/":
        z = x / y
        w = str(z)
        myresult.set(w)

def refresh():
    global x, y, oper
    x,y,oper = None,None,None

root = Tk()
root.title("My Calc")
root.geometry("200x250+500+200")
root.iconbitmap(bitmap="C:\\Users\\lenovo\\Desktop\\mycalc.ico")
myresult = StringVar()
myresult.set("0")
frame = Frame(root)
frame.pack()

result = Label(frame,state=DISABLED,height=1,relief=SUNKEN,textvariable=myresult,font=('Courier',-42, 'bold')).pack(expand=1,fill=BOTH)

frame1 = Frame(frame)
frame3 = Frame(frame)
frame2 = Frame(frame)
frame4 = Frame(frame)
frame1.pack(fill=BOTH)
frame3.pack(fill=BOTH)
frame2.pack(fill=BOTH)
frame4.pack(fill=BOTH)

ttk.Separator(frame,orient=HORIZONTAL).pack(side=BOTTOM) //this is not working too

number_0 = Button(frame1, width=4, height=2,text='0',command = var_val(0))
number_0.pack(side=LEFT,padx=2,pady=2)
number_1 = Button(frame1, width=4, height=2,text='1', command = var_val(1))
number_1.pack(side=LEFT,padx=2,pady=2)
number_2 = Button(frame1, width=4, height=2,text='2', command = var_val(2))
number_2.pack(side=LEFT,padx=2,pady=2)
number_3 = Button(frame1, width=4, height=2,text='3', command = var_val(3))
number_3.pack(side=LEFT,padx=2,pady=2)
number_4 = Button(frame3, width=4, height=2,text='4', command = var_val(4))
number_4.pack(side=LEFT,padx=2,pady=2)
number_5 = Button(frame3, width=4, height=2,text='5', command = var_val(5))
number_5.pack(side=LEFT,padx=2,pady=2)
number_6 = Button(frame3, width=4, height=2,text='6', command = var_val(6))
number_6.pack(side=LEFT,padx=2,pady=2)
number_7 = Button(frame3, width=4, height=2,text='7', command = var_val(7))
number_7.pack(side=LEFT,padx=2,pady=2)
number_8 = Button(frame2, width=4, height=2,text='8', command = var_val(8))
number_8.pack(side=LEFT,padx=2,pady=2)
number_9 = Button(frame2, width=4, height=2,text='9', command = var_val(9))
number_9.pack(side=LEFT,padx=2,pady=2)

add_sign = Button(frame2, width=4, height=2,text='+', command = oper_val('+'))
add_sign.pack(side=LEFT,padx=2,pady=2)
sub_sign = Button(frame2, width=4, height=2,text='-', command = oper_val('-'))
sub_sign.pack(side=LEFT,padx=2,pady=2)
mul_sign = Button(frame4, width=4, height=2,text='*', command = oper_val('*'))
mul_sign.pack(side=LEFT,padx=2,pady=2)
div_sign = Button(frame4, width=4, height=2,text='/', command = oper_val('/'))
div_sign.pack(side=LEFT,padx=2,pady=2)
eq_sign = Button(frame4, width=4, height=2,text='=', command = get_result)
eq_sign.pack(side=LEFT,padx=2,pady=2)
ce_sign = Button(frame4, width=4, height=2,text='CE', command = refresh)
ce_sign.pack(side=LEFT,padx=2,pady=2)

root.mainloop()

0 个答案:

没有答案