我正在尝试使用tkinter在python中创建一个图形计算器。我有一个标签,用于显示用户正在绘图的等式,我想每次点击一个按钮时更新标签。我对tkinter很新,我面临的一个问题是,当程序循环时,它刷新了我必须遵循的初始条件列表。有没有办法使用tkinter mainloop只循环程序的一部分,以便我的列表停止刷新?非常感谢你!我尽我所能!
from tkinter import *
import math
mat=["hello"]
root = Tk()
height=400
width=420
buttonframe=Frame(root)
buttonframe.pack(side=BOTTOM)
canvas = Canvas(height=height, width=width, bg="black")
canvas.pack(anchor=NW)
w=Label(buttonframe,bg="white",text="y = " + (''.join(mat)))
w.grid(row=0,column=3)
def button0():
mat.append("0")
def button1():
mat.append("1")
def button2():
mat.append("2")
def button3():
mat.append("3")
def button4():
mat.append("4")
def button5():
mat.append("5")
def button6():
mat.append("6")
def button7():
mat.append("7")
def button8():
mat.append("8")
def button9():
mat.append("9")
def buttonsin(var):
mat.append("(math.sin("+ var +"))")
def buttoncos(var):
mat.append("(mat.cos("+ var + "))")
def buttontan():
mat.append("(mat.tan("+ var + "))")
for x in range(-(int(width/2)),(int(width/2))):
x1=(width/2)+x
x2=(width/2)+x+1
y = (height/2)-(math.sin(x))
y2= (height/2)-(math.sin(x+1))
canvas.create_line(x1,y,x2,y2,fill="green",dash=(5,5))
#buttons
button1 = Button(buttonframe, text="0", width=10,height=2,bg="light blue",command=button0)
button1.grid(row=1,column=3)
button2 = Button(buttonframe, text="1", width=10,height=2,bg="light blue",command=button1)
button2.grid(row=1,column=4)
button3 = Button(buttonframe,text="2", width=10,height=2,bg="light blue",command=button2)
button3.grid(row=1,column=5)
button4 = Button(buttonframe,text="sine", width=10,height=2,bg="light blue",command=buttonsin)
button4.grid(row=1,column=6)
button5 = Button(buttonframe,text="cosine", width=10,height=2,bg="light blue",command=buttoncos)
button5.grid(row=1,column=7)
button6 = Button(buttonframe,text="tangent", width=10,height=2,bg="light blue",command=buttontan)
button6.grid(row=1,column=8)
#next row
button7 = Button(buttonframe,text="3", width=10,height=2,bg="light blue",command=button3)
button7.grid(row=2,column=3)
button1 = Button(buttonframe, text="4", width=10,height=2,bg="light blue",command=button4)
button1.grid(row=2,column=4)
button2 = Button(buttonframe, text="5", width=10,height=2,bg="light blue",command=button5)
button2.grid(row=2,column=5)
button3 = Button(buttonframe,text="x^2", width=10,height=2,bg="light blue")
button3.grid(row=2,column=6)
button4 = Button(buttonframe,text="x^y", width=10,height=2,bg="light blue")
button4.grid(row=2,column=7)
button5 = Button(buttonframe,text="sqrt", width=10,height=2,bg="light blue")
button5.grid(row=2,column=8)
#next row
button6 = Button(buttonframe,text="6", width=10,height=2,bg="light blue",command=button6)
button6.grid(row=3,column=3)
button7 = Button(buttonframe,text="7", width=10,height=2,bg="light blue",command=button7)
button7.grid(row=3,column=4)
button5 = Button(buttonframe,text="8", width=10,height=2,bg="light blue",command=button8)
button5.grid(row=3,column=5)
button6 = Button(buttonframe,text="button6", width=10,height=2,bg="light blue")
button6.grid(row=3,column=6)
button7 = Button(buttonframe,text="button7", width=10,height=2,bg="light blue")
button7.grid(row=3,column=7)
button1 = Button(buttonframe, text="button", width=10,height=2,bg="light blue")
button1.grid(row=3,column=8)
#next row
button2 = Button(buttonframe, text="9", width=10,height=2,bg="light blue",command=button9)
button2.grid(row=4,column=3)
button3 = Button(buttonframe,text="+", width=10,height=2,bg="light blue")
button3.grid(row=4,column=4)
button4 = Button(buttonframe,text="-", width=10,height=2,bg="light blue")
button4.grid(row=4,column=5)
button5 = Button(buttonframe,text="button5", width=10,height=2,bg="light blue")
button5.grid(row=4,column=6)
button6 = Button(buttonframe,text="button6", width=10,height=2,bg="light blue")
button6.grid(row=4,column=7)
button6 = Button(buttonframe,text="button6", width=10,height=2,bg="light blue")
button6.grid(row=4,column=8)
root.mainloop()
答案 0 :(得分:2)
mainloop
不会遍历您的代码。你的程序执行一次。 mainloop
只是一个处理队列中事件的循环,它不会不断尝试运行您的代码。
问题不在于列表没有更新。列表正在进行更新。您没有做任何事情导致显示该列表。配置标签以显示列表时,最终会为其提供静态字符串。您需要显式重置标签上的字符串。
def button0():
mat.append("0")
w.configure(text="y = " + (''.join(mat)))
答案 1 :(得分:1)
Tkinter的主循环检查新触发事件等更新,它不会反复执行所有代码。关键不是改变mainloop的行为,而是确保初始化代码不是由循环运行的。
试试这样的事情
def button9():
mat.append("9")
w.config(text="y = " + (''.join(mat)))
答案 2 :(得分:-1)
一种解决方案是将您的Tkinter创建为一个类。这样,当你需要更新它更新的值时,只要实例存在就更新所有存在的类变量的值