gpt4 book ai didi

python - Tkinter 理解 mainloop

转载 作者:IT老高 更新时间:2023-10-28 20:37:52 32 4
gpt4 key购买 nike

到现在为止,我曾经用 tk.mainloop() 结束我的 Tkinter 程序,否则什么都不会出现!见例子:

from Tkinter import *
import random
import time

tk = Tk()
tk.title = "Game"
tk.resizable(0,0)
tk.wm_attributes("-topmost", 1)

canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()

class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
def draw(self):
pass

ball = Ball(canvas, "red")

tk.mainloop()

但是,当尝试该程序的下一步(使球按时间移动)时,正在阅读的书说要执行以下操作。所以我把draw函数改成:

def draw(self):
self.canvas.move(self.id, 0, -1)

并将以下代码添加到我的程序中:

while 1:
ball.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)

但我注意到添加这段代码,使得 tk.mainloop() 的使用变得毫无用处,因为即使没有它,一切都会显示出来!!!

此时我应该提一下,我的书从不谈论 tk.mainloop()(可能是因为它使用了 Python 3),但我是在网上搜索了解它的,因为我的程序没有不能通过复制书的代码来工作!

所以我尝试了以下行不通的操作!!!

while 1:
ball.draw()
tk.mainloop()
time.sleep(0.01)

发生了什么事? tk.mainloop() 是什么? tk.update_idletasks()tk.update() 有什么作用,它们与 tk.mainloop() 有何不同?我应该使用上面的循环吗?tk.mainloop()?还是两者都在我的程序中?

最佳答案

tk.mainloop() block 。这意味着您的 Python 命令的执行将在那里停止。你可以通过写来看到:

while 1:
ball.draw()
tk.mainloop()
print("hello") #NEW CODE
time.sleep(0.01)

您永远不会看到 print 语句的输出。因为没有循环,所以球不会移动。

另一方面,update_idletasks()update() 方法在这里:

while True:
ball.draw()
tk.update_idletasks()
tk.update()

...不要阻止;在这些方法完成后,将继续执行,因此 while 循环将一遍又一遍地执行,从而使球移动。

包含方法调用 update_idletasks()update() 的无限循环可以替代调用 tk.mainloop() .请注意,整个 while 循环可以说是 block 就像 tk.mainloop() 因为在 while 循环之后什么都不会执行。

但是,tk.mainloop() 并不能仅替代这些行:

tk.update_idletasks()
tk.update()

相反,tk.mainloop() 是整个 while 循环的替代品:

while True:
tk.update_idletasks()
tk.update()

对评论的回应:

这里是tcl docs说:

Update idletasks

This subcommand of update flushes all currently-scheduled idle eventsfrom Tcl's event queue. Idle events are used to postpone processinguntil “there is nothing else to do”, with the typical use case forthem being Tk's redrawing and geometry recalculations. By postponingthese until Tk is idle, expensive redraw operations are not done untileverything from a cluster of events (e.g., button release, change ofcurrent window, etc.) are processed at the script level. This makes Tkseem much faster, but if you're in the middle of doing some longrunning processing, it can also mean that no idle events are processedfor a long time. By calling update idletasks, redraws due to internalchanges of state are processed immediately. (Redraws due to systemevents, e.g., being deiconified by the user, need a full update to beprocessed.)

APN As described in Update considered harmful, use of update to handleredraws not handled by update idletasks has many issues. Joe Englishin a comp.lang.tcl posting describes an alternative:

所以 update_idletasks() 会导致处理一些由 update() 导致的事件子集。

来自 update docs :

update ?idletasks?

The update command is used to bring the application “up to date” byentering the Tcl event loop repeatedly until all pending events(including idle callbacks) have been processed.

If the idletasks keyword is specified as an argument to the command,then no new events or errors are processed; only idle callbacks areinvoked. This causes operations that are normally deferred, such asdisplay updates and window layout calculations, to be performedimmediately.

KBK (12 February 2000) -- My personal opinion is that the [update]command is not one of the best practices, and a programmer is welladvised to avoid it. I have seldom if ever seen a use of [update] thatcould not be more effectively programmed by another means, generallyappropriate use of event callbacks. By the way, this caution appliesto all the Tcl commands (vwait and tkwait are the other commonculprits) that enter the event loop recursively, with the exception ofusing a single [vwait] at global level to launch the event loop insidea shell that doesn't launch it automatically.

The commonest purposes for which I've seen [update] recommended are:

  1. Keeping the GUI alive while some long-running calculation isexecuting. See Countdown program for an alternative. 2) Waiting for a window to be configured before doing things likegeometry management on it. The alternative is to bind on events suchas that notify the process of a window's geometry. SeeCentering a window for an alternative.

What's wrong with update? There are several answers. First, it tendsto complicate the code of the surrounding GUI. If you work theexercises in the Countdown program, you'll get a feel for how mucheasier it can be when each event is processed on its own callback.Second, it's a source of insidious bugs. The general problem is thatexecuting [update] has nearly unconstrained side effects; on returnfrom [update], a script can easily discover that the rug has beenpulled out from under it. There's further discussion of thisphenomenon over at Update considered harmful.

.....

Is there any chance I can make my program work without the while loop?

是的,但事情变得有点棘手。您可能会认为以下内容会起作用:

class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)

def draw(self):
while True:
self.canvas.move(self.id, 0, -1)

ball = Ball(canvas, "red")
ball.draw()
tk.mainloop()

问题是 ball.draw() 会导致执行进入 draw() 方法中的无限循环,因此 tk.mainloop() 将永远不会执行,并且您的小部件将永远不会显示。在 gui 编程中,必须不惜一切代价避免无限循环,以保持小部件对用户输入的响应,例如鼠标点击。

所以,问题是:你如何一遍又一遍地执行某事而不实际创建无限循环? Tkinter 对这个问题有一个答案:一个小部件的 after() 方法:

from Tkinter import *
import random
import time

tk = Tk()
tk.title = "Game"
tk.resizable(0,0)
tk.wm_attributes("-topmost", 1)

canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()

class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)

def draw(self):
self.canvas.move(self.id, 0, -1)
self.canvas.after(1, self.draw) #(time_delay, method_to_execute)




ball = Ball(canvas, "red")
ball.draw() #Changed per Bryan Oakley's comment
tk.mainloop()

after() 方法不会阻塞(它实际上创建了另一个执行线程),因此在调用 after() 之后,python 程序中的执行将继续,这意味着 tk.mainloop( ) 接下来执行,因此您的小部件得到配置和显示。 after() 方法还允许您的小部件保持对其他用户输入的响应。尝试运行以下程序,然后在 Canvas 上的不同位置单击鼠标:

from Tkinter import *
import random
import time

root = Tk()
root.title = "Game"
root.resizable(0,0)
root.wm_attributes("-topmost", 1)

canvas = Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()

class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)

self.canvas.bind("<Button-1>", self.canvas_onclick)
self.text_id = self.canvas.create_text(300, 200, anchor='se')
self.canvas.itemconfig(self.text_id, text='hello')

def canvas_onclick(self, event):
self.canvas.itemconfig(
self.text_id,
text="You clicked at ({}, {})".format(event.x, event.y)
)

def draw(self):
self.canvas.move(self.id, 0, -1)
self.canvas.after(50, self.draw)




ball = Ball(canvas, "red")
ball.draw() #Changed per Bryan Oakley's comment.
root.mainloop()

关于python - Tkinter 理解 mainloop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29158220/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com