了解 Tkinter mainloop() 的逻辑以及为什么变量没有重新分配其原始值? [英] Understanding the logic of Tkinter mainloop() and why variables are not reassigned their original values?

查看:22
本文介绍了了解 Tkinter mainloop() 的逻辑以及为什么变量没有重新分配其原始值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的理解 window.mainloop() 不断重复 GUI 代码,以便窗口及其小部件留在屏幕上.因此,为什么变量(例如 canvastext)可以更新并保持更新?window.mainloop() 的逻辑肯定会覆盖 canvastext 以便它再次具有文本值 'Hi' 而不是新的 spinbox 值?可能是我完全误解了 window.mainloop() 的作用,但如果它确实使程序不断循环代码,那么为什么变量没有重新分配其原始值?

From my understanding window.mainloop() keeps repeating the GUI code so that the window and its widgets stay on the screen. Why, therefore, can a variable (such as canvastext) be updated and stay updated? Surely the logic of window.mainloop() would overwrite canvastext so that it once again has the text value 'Hi' instead of the new spinbox value? It may be that I have completely misunderstood what window.mainloop() does but if it does make the program keep looping through the code then why aren't variables reassigned their original values?

from tkinter import *

x = 10
y = 10
a = 100
b = 100

def hello():
    #print spin value
    print ("Spin Value:")
    number = v.get()
    print(number)
    #update text with variable value
    txt = v.get()
    global canvastext
    canvas1.delete(canvastext)
    canvas1.update()
    canvastext = canvas1.create_text(50, 50, text = txt)

window = Tk()
window.geometry("500x500")

#canvas and drawing
canvas1=Canvas(window, height = 200, width = 400)
canvas1.grid(row=0, column=0, sticky=W)
coord = [x, y, a, b]
rect = canvas1.create_rectangle(*coord, outline="#fb0", fill="#fb0")
canvastext = canvas1.create_text(50, 50, text ="Hi")

# create a toplevel menu
menubar = Menu(window)

firstmenu = Menu(menubar, tearoff=0)
firstmenu.add_command(label="Hello!", command=hello)
firstmenu.add_command(label="Quit!", command=window.destroy)
menubar.add_cascade(label="Menu1", menu=firstmenu)

secondmenu = Menu(menubar, tearoff=0)
secondmenu.add_command(label="Hi!", command=hello)
secondmenu.add_command(label="Quit!", command=window.destroy)
menubar.add_cascade(label="Menu2", menu=secondmenu)

window.config(menu=menubar)

#spinboxes and capturing value
v=IntVar()
spin = Spinbox(window, textvariable=v, from_=1, to = 10)
spin.grid(row=1, column = 0, sticky= W)

window.mainloop()

推荐答案

"根据我的理解 window.mainloop() 不断重复 GUI 代码以便窗口及其小部件留在屏幕上"

"From my understanding window.mainloop() keeps repeating the GUI code so that the window and its widgets stay on the screen"

不完全是.它不会不断重复"任何事情,只是不断寻找事件并为事件调度处理程序.它不会重新运行您的任何代码.mainloop 很简单,和这个没什么区别:

Not exactly. It doesn't "keep repeating" anything, except to keep looking for events and dispatching the handlers for the events. It doesn't re-run any of your code. mainloop is very simple, it's not much different than this:

while (the_main_window_hasnt_been_destroyed):
    event=event_queue.pop()
    event.handle()

我不明白你关于变量被重置的问题.事件循环本身不会改变任何东西.它所做的只是等待事件,然后调用适当的处理程序.如果处理程序更改了一个变量,它就会被更改.如果没有,它保持不变.

I don't understand your question about variables getting reset. The event loop by itself does not alter anything. All it does is wait for events and then call the appropriate handler. If a handler changes a variable, it gets changed. If not, it stays the same.

这篇关于了解 Tkinter mainloop() 的逻辑以及为什么变量没有重新分配其原始值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆