【发布时间】:2017-06-14 19:10:20
【问题描述】:
我正在尝试使用 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()
【问题讨论】:
-
真正的问题是什么?不知道您所说的仅循环程序的一部分是什么意思。
mainloop指的是事件循环,它不会循环你的代码。 -
我不知道,只是开头的 mat = [] 会不断被清除,即使我在上面附加了一些东西,我很确定,因为我正在循环那条线,这初始化它,如果我错了告诉我?
-
为什么我的列表没有更新?请问?
-
mainloop 不会遍历代码的任何部分。
标签: python loops tkinter label updates