【问题标题】:Python returns this error when I quit my window without use the button 'arret'当我退出窗口而不使用按钮 \'arret\' 时,Python 返回此错误
【发布时间】:2022-10-16 04:15:21
【问题描述】:
from tkinter import *
from tkinter.ttk import *
import random as rd
import math as mt
import time

fin = 0

def touche (event):
    global xa, xb, ya, yb
    key = event.char
    print(key)

def arret ():
    global fin
    fin = 1
    
def boucle ():
    global a,b,xa,xb,ya,yb,x0,y0,x1,y1,r
    x = xa+10
    y = ya
    r = (xb-xa)/2
    for i in range (0, len(a)):
        x0 = x-a[i]*r
        y0 = y-b[i]*r
        x1 = x+a[i]*r
        y1 = y+b[i]*r
        can.coords(helice,x0, y0, x1, y1)
        can.update()
        time.sleep(0.02)
        
fen = Tk()

fin = 0

key = 'v'

can = Canvas(fen, bg = 'deep sky blue', width = 200, height = 200)
can.pack()

quit = Button(fen, text = 'quit', command = fen.destroy)
quit.pack(side = BOTTOM)

xa = rd.randint(5, 195)
ya = rd.randint(5, 195)
xb = xa+20
yb = ya

list_color = ['black', 'lavender blush', 'aquamarine', 'blue violet', 'tomato', 'dark cyan', 'green yellow', 
              'medium spring green']
number = rd.randint(0,7)
for i in list_color:
    if list_color.index(i)==number:
        color = i
    
helice = can.create_line(xa, ya, xb, yb, width = 4, fill = color)

arret = Button(fen, text = 'arret', command = arret)
arret.pack(side = BOTTOM)

a = [-1, 0, 1, 1, 1, 0, -1, -1]
b = [-1, -1, -1, 0, 1, 1, 1, 0]

while fin != 1:
    boucle()

    
fen.bind ('<Key>', touche)
 
fen.mainloop()

回报 :

TclError                                  Traceback (most recent call last)
Input In [71], in <cell line: 64>()
     62 b = [-1, -1, -1, 0, 1, 1, 1, 0]
     64 while fin != 1:
---> 65     boucle()
     68 fen.bind ('<Key>', touche)
     70 fen.mainloop()

Input In [71], in boucle()
     26 x1 = x+a[i]*r
     27 y1 = y+b[i]*r
---> 28 can.coords(helice,x0, y0, x1, y1)
     29 can.update()
     30 time.sleep(0.02)

File ~\anaconda3\lib\tkinter\__init__.py:2766, in Canvas.coords(self, *args)
   2762 """Return a list of coordinates for the item given in ARGS."""
   2763 # XXX Should use _flatten on args
   2764 return [self.tk.getdouble(x) for x in
   2765                    self.tk.splitlist(
-> 2766            self.tk.call((self._w, 'coords') + args))]

TclError: invalid command name ".!canvas"

我试图创建一个函数来解决这个问题,但它不起作用。 如果我单击名为“arret”的按钮,Python 不会向我显示此错误,而是我必须做的下一个练习。我必须添加一个在“点击”之前无法运行的键盘事件。

我认为问题出现在我的 while 循环中,但我不明白为什么。

【问题讨论】:

    标签: python-3.x tkinter while-loop tkinter-canvas coords


    【解决方案1】:
    from tkinter import *
    from tkinter.ttk import *
    import random as rd
    import math as mt
    import time
    
    fin = 0
    
    def touche (event):
        global xa, xb, ya, yb
        key = event.char
        print(key)
    
    def arret ():
        global fin
        fin = 1
        
    def boucle ():
        global a,b,xa,xb,ya,yb,x0,y0,x1,y1,r,fin
        try:
            while(fin !=1):
                x = xa+10
                y = ya
                r = (xb-xa)/2
                for i in range (0, len(a)):
                    x0 = x-a[i]*r
                    y0 = y-b[i]*r
                    x1 = x+a[i]*r
                    y1 = y+b[i]*r
                    can.coords(helice,x0, y0, x1, y1)
                    can.update()
                    time.sleep(0.02)
        except:
            pass
            
    fen = Tk()
    
    fin = 0
    
    key = 'v'
    
    can = Canvas(fen, bg = 'deep sky blue', width = 200, height = 200)
    can.pack()
    
    quit = Button(fen, text = 'quit', command = fen.destroy)
    quit.pack(side = BOTTOM)
    
    xa = rd.randint(5, 195)
    ya = rd.randint(5, 195)
    xb = xa+20
    yb = ya
    
    list_color = ['black', 'lavender blush', 'aquamarine', 'blue violet', 'tomato', 'dark cyan', 'green yellow', 
                  'medium spring green']
    number = rd.randint(0,7)
    for i in list_color:
        if list_color.index(i)==number:
            color = i
        
    helice = can.create_line(xa, ya, xb, yb, width = 4, fill = color)
    
    arret = Button(fen, text = 'arret', command = arret)
    arret.pack(side = BOTTOM)
    
    a = [-1, 0, 1, 1, 1, 0, -1, -1]
    b = [-1, -1, -1, 0, 1, 1, 1, 0]
    
    fen.after(0, boucle)
      
    fen.bind ('<Key>', touche)
     
    fen.mainloop()
    

    或者您可以使用以下代码:

    from tkinter import *
    from tkinter.ttk import *
    import random as rd
    import math as mt
    import time
    
    fin = 0
    
    def touche (event):
        global xa, xb, ya, yb
        key = event.char
        print(key)
    
    def arret ():
        global fin
        fin = 1
        
    def boucle ():
        global a,b,xa,xb,ya,yb,x0,y0,x1,y1,r,fin
        
        while(fin !=1):
            x = xa+10
            y = ya
            r = (xb-xa)/2
            for i in range (0, len(a)):
                x0 = x-a[i]*r
                y0 = y-b[i]*r
                x1 = x+a[i]*r
                y1 = y+b[i]*r
                if(fin!=1):
                    can.coords(helice,x0, y0, x1, y1)
                    can.update()
                    time.sleep(0.02)
       
    def die():
        global fin
        fin=1
        fen.quit()
        fen.destroy()        
    fen = Tk()
    
    fin = 0
    
    key = 'v'
    
    can = Canvas(fen, bg = 'deep sky blue', width = 200, height = 200)
    can.pack()
    
    quit = Button(fen, text = 'quit', command = die)
    quit.pack(side = BOTTOM)
    
    fen.protocol("WM_DELETE_WINDOW", die)
    
    xa = rd.randint(5, 195)
    ya = rd.randint(5, 195)
    xb = xa+20
    yb = ya
    
    list_color = ['black', 'lavender blush', 'aquamarine', 'blue violet', 'tomato', 'dark cyan', 'green yellow', 
                  'medium spring green']
    number = rd.randint(0,7)
    for i in list_color:
        if list_color.index(i)==number:
            color = i
        
    helice = can.create_line(xa, ya, xb, yb, width = 4, fill = color)
    
    arret = Button(fen, text = 'arret', command = arret)
    arret.pack(side = BOTTOM)
    
    a = [-1, 0, 1, 1, 1, 0, -1, -1]
    b = [-1, -1, -1, 0, 1, 1, 1, 0]
    
    fen.after(0, boucle)
      
    fen.bind ('<Key>', touche)
     
    fen.mainloop()
    

    玩得开心 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-01
      • 1970-01-01
      相关资源
      最近更新 更多