日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2025/3/21 python 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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”命令:应用程序已被破坏错误 - 堆栈内存溢出...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。