【问题标题】:tkinker - Can i avoid looping through the entire program?tkinter - 我可以避免循环整个程序吗?
【发布时间】: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


【解决方案1】:

mainloop 不会循环遍历您的代码。您的程序执行一次。 mainloop 只是一个处理队列中事件的循环,它不会不断尝试运行您的代码。

问题不在于列表没有更新。列表正在更新。您没有做任何事情来导致显示该列表。当你配置一个标签来显示列表时,你最终是给它一个静态字符串。您需要显式重置标签上的字符串。

def button0():
    mat.append("0")
    w.configure(text="y = " + (''.join(mat)))

【讨论】:

  • 啊啊啊!!!谢谢!!!!!!!!!!!!我对此很陌生,这是有道理的!!!另一个人也超级乐于助人!!!谢谢!!
【解决方案2】:

Tkinter 的主循环会检查更新,例如新触发的事件,它不会一遍又一遍地执行您的所有代码。关键不是改变主循环的行为,而是确保初始化代码不是由循环运行的。

试试这样的

def button9():
  mat.append("9")
  w.config(text="y = " + (''.join(mat)))

【讨论】:

  • 谢谢!但确保初始化代码不是由循环运行的?
  • 这个措辞不正确。 mainloop 不会“通过程序检查更新”。它只是一个循环;和while True: process_event()没有太大区别。
  • 如果不是这样,那你为什么认为我的列表(垫)没有更新??? @BryanOakley
  • 您的垫子列表实际上如何更新您的标签?那是我没有看到的部分。你说得对,布莱恩,它的措辞是错误的。
  • 看来在调试过程中我删除了那部分,哎呀......但它所做的是将列表作为字符串加入并直接显示在标签中
【解决方案3】:

一种解决方案是将您的 Tkinter 创建为一个类。这样,当您需要更新值时,它会更新以更新类变量的值,只要实例存在,这些变量都将存在

【讨论】:

  • 您并没有通过建议使用类来解决 OPs 问题。 OP 试图做的事情完全有可能在他以错误的方式进行的课程之外。
  • ^^^^^^^*****she
  • @smaude:对不起,我应该让我的 cmets 保持性别中立。习惯的力量。
猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 2020-05-27
  • 2017-08-12
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多