tkinter - 我可以避免循环遍历整个程序吗?
tkinker - Can i avoid looping through the entire program?
我正在尝试使用 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()
Tkinter 的主循环检查新触发事件等更新,它不会一遍又一遍地执行所有代码。关键不是改变 mainloop 的行为,而是确保初始化代码不是 运行 循环。
试试这样的方法
def button9():
mat.append("9")
w.config(text="y = " + (''.join(mat)))
一个解决方案是将 Tkinter 创建为 class。这样,当您需要更新值时,它会更新以更新 class 变量的值,只要实例存在
,这些变量就会全部存在
mainloop
不会循环遍历您的代码。你的程序只执行一次。 mainloop
只是一个处理队列中事件的循环,它不会连续尝试 运行 您的代码。
问题不在于列表没有更新。正在更新列表 。您没有做任何事情来显示该列表。当你配置一个标签来显示列表时,你最终是给它一个静态字符串。您需要明确重置标签上的字符串。
def button0():
mat.append("0")
w.configure(text="y = " + (''.join(mat)))
我正在尝试使用 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()
Tkinter 的主循环检查新触发事件等更新,它不会一遍又一遍地执行所有代码。关键不是改变 mainloop 的行为,而是确保初始化代码不是 运行 循环。
试试这样的方法
def button9():
mat.append("9")
w.config(text="y = " + (''.join(mat)))
一个解决方案是将 Tkinter 创建为 class。这样,当您需要更新值时,它会更新以更新 class 变量的值,只要实例存在
,这些变量就会全部存在mainloop
不会循环遍历您的代码。你的程序只执行一次。 mainloop
只是一个处理队列中事件的循环,它不会连续尝试 运行 您的代码。
问题不在于列表没有更新。正在更新列表 。您没有做任何事情来显示该列表。当你配置一个标签来显示列表时,你最终是给它一个静态字符串。您需要明确重置标签上的字符串。
def button0():
mat.append("0")
w.configure(text="y = " + (''.join(mat)))