欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python升级命令出现错误_python - _tkinter.TclError:无法调用“ update”命令:应用程序已被破坏错误 - 堆栈内存溢出...

发布时间:2025/3/21 python 54 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python升级命令出现错误_python - _tkinter.TclError:无法调用“ update”命令:应用程序已被破坏错误 - 堆栈内存溢出... 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

我同意其他人的看法,您应该在这里使用mainloop() ,但是,如果您想保留原始代码的方式是跟踪布尔值, while x == True 。 这样,我们可以将x的值更新为False ,这样就可以避免发生错误。

当应用关闭时,我们可以使用protocol()方法更新布尔值。

如果我们将其添加到您的代码中:

x = True

def update_x():

global x

x = False

tk.protocol("WM_DELETE_WINDOW", update_x)

并将您的while语句更改为:

while x == True:

tk.update_idletasks()

tk.update()

time.sleep(0.01)

因此,您的完整代码可能如下所示:

from tkinter import *

import random

import time

tk=Tk()

tk.title("My 21st Century Pong Game")

tk.resizable(0,0)

tk.wm_attributes("-topmost",1)

x = True

def update_x():

global x

x = False

tk.protocol("WM_DELETE_WINDOW", update_x)

canvas=Canvas(tk,bg="white",width=500,height=400,bd=0,highlightthickness=0)

canvas.pack()

tk.update()

class Ball:

def __init__(self,canvas,color):

self.canvas=canvas

self.id=canvas.create_oval(30,30,50,50,fill=color)

""" Note: x and y coordinates for top left corner and x and y coordinates for the bottom right corner, and finally the fill colour for the oval

"""

self.canvas.move(self.id,0,0)

def draw(self):

pass

ball1=Ball(canvas,'green')

while x == True:

tk.update_idletasks()

tk.update()

time.sleep(0.01)

这将解决您的问题。

要重申其他人所说的,您真正需要的mainloop()此处的mainloop()而不是您的while i:语句。

mainloop()方法用于重置Tk()实例的循环。一旦代码到达显示tk.mainloop()的行,它将成为代码的下一个循环。

编写代码的正确方法是只使用mainloop()因为它会对mainloop()实例进行所有更新。

请参阅以下使用mainloop()代码:

from tkinter import *

tk=Tk()

tk.title("My 21st Century Pong Game")

tk.resizable(0,0)

tk.wm_attributes("-topmost",1)

canvas=Canvas(tk,bg="white",width=500,height=400,bd=0,highlightthickness=0)

canvas.pack()

tk.update()

class Ball:

def __init__(self,canvas,color):

self.canvas=canvas

self.id=canvas.create_oval(30,30,50,50,fill=color)

""" Note: x and y coordinates for top left corner and x and y coordinates for the bottom right corner, and finally the fill colour for the oval

"""

self.canvas.move(self.id,0,0)

def draw(self):

pass

ball1=Ball(canvas,'green')

tk.mainloop()

总结

以上是生活随笔为你收集整理的python升级命令出现错误_python - _tkinter.TclError:无法调用“ update”命令:应用程序已被破坏错误 - 堆栈内存溢出...的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。