简体   繁体   English

tkinter 代码是如何执行的?

[英]How is tkinter code executed?

I am writing a program using tkinter, but I do not understand how it works.我正在使用 tkinter 编写程序,但我不明白它是如何工作的。 Normally, code is executed top-down, but with tkinter it obviously does not.通常,代码是自上而下执行的,但 tkinter 显然不是。

For example, I have bound a function to the left mouse button, and this function is executed every time I click the button.比如我给鼠标左键绑定了一个函数,每次点击按钮都会执行这个函数。 But how is the other code around that treated?但是周围的其他代码是如何处理的呢? My problem is that I in the start of my program initialize a variable that is used as an argument in the bound function, and then it is changed in the function and returned.我的问题是我在程序开始时初始化了一个变量,该变量在绑定函数中用作参数,然后在函数中更改并返回。 But every time the function is called, the variable seems to be reset to its initial value.但是每次调用函数时,变量似乎都被重置为其初始值。

Does anyone know why this is?有人知道为什么是这样吗?

I have it written like this:我是这样写的:

var = "black"
var = c.bind("<Button-1>", lambda event: func(event, arg=var))

The function "func" changes var and returns it, but the next time I press the button the variable is always "black".函数“func”更改 var 并返回它,但下次我按下按钮时,变量总是“黑色”。

Thanks in advance!提前致谢!

Tkinter does indeed run top down. Tkinter 确实自上而下运行。 What makes tkinter different is what happens when it gets to the bottom.使 tkinter 与众不同的是当它到达底部时会发生什么。

Typically, the last executable statement in a tkinter program is a call to the mainloop method of the root window.通常, mainloop程序中的最后一条可执行语句是对根窗口的mainloop方法的调用。 Roughtly speaking, tkinter programs look like this:粗略地说,tkinter 程序如下所示:

# top of the program logic
root = tkinter.Tk()
...
def some_function(): ...
...
some_widget.bind("<1>", some_function)
...
# bottom of the program logic
root.mainloop()

mainloop is just a relatively simple infinite loop. mainloop只是一个相对简单的无限循环。 You can think of it as having the following structure:您可以将其视为具有以下结构:

while the_window_has_not_been_destroyed():
    event = wait_for_next_event()
    process_event(event)

The program is in a constant state of waiting.程序一直处于等待状态。 It waits for an event such as a button click or key click, and then processes that event.它等待诸如按钮单击或按键单击之类的事件,然后处理该事件。 Conceptually, it processes the event by scanning a table to find if that event has been associated with the widget that caught the event.从概念上讲,它通过扫描表格来处理事件,以查找该事件是否与捕获该事件的小部件相关联。 If it finds a match, it runs the command that is bound to that widget+event combination.如果找到匹配项,它将运行绑定到该小部件+事件组合的命令。

When you set up a binding or associate a command with a button, you are adding something to that table.当您设置绑定或将命令与按钮相关联时,您正在向该表中添加一些内容。 You are telling tkinter "if event X happens on widget Y, run function Z".您告诉 tkinter“如果小部件 Y 上发生事件 X,则运行函数 Z”。

You can't use a return result because it's not your code that is calling this function.您不能使用返回结果,因为调用此函数的不是您的代码。 The code that calls the function is mainloop , and it doesn't care what the function returns.调用函数的代码是mainloop ,它不关心函数返回什么。 Anything that gets returned is simply ignored.任何返回的东西都会被忽略。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM