- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
到现在为止,我曾经用 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:
- 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/
在 How to make a Tkinter window jump to the front? 中提出的问题之后的一个问题 我希望有一个顶层窗口(我用它来导航我的其他主窗口)总是在前面。但我希望它
有没有办法在 Tkinter 中保持小部件(特别是图像)的堆叠顺序一致?例如,我可能在 Canvas 上的同一位置有两个矩形、两个三角形和一个圆。圆圈移动到最后一次点击鼠标的地方,但我总是希望它被绘制
这是一个简单的 GUI 程序,用于创建 5x16 按钮矩阵。 from tkinter import * root = Tk() button = [[0 for x in range(16)] fo
有一个错误:“AttributeError: module 'tkinter' has no attribute 'messagebox'” 即使 import tkinter 一开始就已经给出了,为
我知道 menu.tk_popup() 可用于在特定坐标处打开上下文菜单,但也不知道如何从中打开子菜单,如果这有意义的话。这是我编写的代码: import tkinter as tk root = t
我正在尝试在禁用自动换行和水平滚动条的文本窗口中书写,如下所示: root = Toplevel() root.geometry("%dx%d+0+0" % (350,400)) af=Frame(r
已经将文本变量分配给小部件后,如何将其删除? widget.config(textvariable=None)只是不工作。在谷歌或这里找不到任何东西。 最佳答案 将您的变量分配给一个空字符串以实现此目
Jython 支持 Tkinter 吗?如果我用 Python 编写一个程序并放一个 使用 Tkinter 的 GUI 前端,做同样的事情有多难 Jython 中的程序?或者对于 Jython GUI
因此,我尝试创建一个 tkinter 窗口,显示当前时间和日期以及自定义短语。不过,我遇到的问题是,我似乎无法在第二天刷新日期。 我可以传递截至运行代码时的当前日期,但之后它变为静态。 这是我目前的程
我的理解是在初始化 __init__ 中的所有框架和小部件之后方法,tkinter 窗口会调整大小以适合所有这些组件。 我想将窗口的初始化大小设置为其最小大小。我希望能够最大化并放大窗口,但我从不希望
此代码仅水平居中,如何使进度条也垂直居中? import Tkinter import ttk root = Tkinter.Tk() root.geometry("=500x500") root.p
使用 Python 2.7 和 Tkinter 模块,我创建了一个菜单按钮并为其分配了一个菜单。现在每次我在特定位置发布菜单时,菜单的宽度都会根据字符数自动设置。有没有办法在菜单小部件中设置静态宽度?
我想将我的 tkinter 应用程序的主题更改为 clam。 代码是什么,我把它放在哪里?我试过了: from tkinter import * from tkinter.ttk import * s
我有以下代码: from Tkinter import * from urllib import urlretrieve import webbrowser import ttk def get_la
我知道,如果我将滚动条控制的框架绑定(bind)到函数 ( onFrameConfigure ),您可以获得滚动条位置,如下所示:self.calendar_frame.bind("", self.o
许多网站都说菜单小部件有一个选项“字体”,但我一直无法设置它。系统是在 Windows 8.1 中运行的 Python 3.5。脚本开始: 从 tkinter 导入 * 根 = Tk() root.g
我正在阅读本教程,它帮助我同时学习 tkinter 和 wxWidgets,但我想深入挖掘,所以想知道哪个 GUI 工具更适合深入学习,为什么? 最佳答案 不可能说哪个“更好”。两者均可用于最常见的用
看书学python,tkinter.END用在一段代码里不用解释 import tkinter def count(text, out_data): """ Update out_data w
我正在尝试使用 Python 2.7 将 Tkinter 导入到我的项目中,但我收到了错误: ImportError: No module named tkinter 在有人说之前,我已经尝试了“Tk
当我回答 Tkinter 问题时,我通常会尝试自己运行代码,但有时我会收到此错误: Traceback (most recent call last): File "C:\Python27\pyg
我是一名优秀的程序员,十分优秀!