【问题标题】:Back Button in Python, I want to go back to the main windowPython中的后退按钮,我想回到主窗口
【发布时间】:2020-04-27 17:08:21
【问题描述】:

我的代码有一些问题。我想制作一个后退按钮,但在显示窗口时出现错误。我的目标是制作一个目录,用户可以在其中按下一些按钮并与产品进行交互。但是当你按下按钮时,这个错误就会出现。

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Users/williamgranizo/.conda/envs/untitled/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/williamgranizo/.conda/envs/untitled/lib/python3.7/site-packages/tkmacosx/basewidget.py", line 255, in cmd
    self.unbind_class('button_command', '<ButtonRelease-1>')
  File "/Users/williamgranizo/.conda/envs/untitled/lib/python3.7/site-packages/tkmacosx/basewidget.py", line 496, in unbind_class
    bindtags = list(self.bindtags())
  File "/Users/williamgranizo/.conda/envs/untitled/lib/python3.7/tkinter/__init__.py", line 1192, in bindtags
    self.tk.call('bindtags', self._w))
_tkinter.TclError: bad window path name ".!toplevel2.!button6"

我不知道主要问题是什么,但主窗口似乎仍然可以工作。我的代码是:

def m1():

ventana.withdraw
modelo_1 = Toplevel()
modelo_1.title("Rose&Frites Hoodie con Gorra")
modelo_1.geometry("700x700")
modelo_1.configure(bg='black')
fn_1 = PhotoImage(file='mblac.png')
lblfn = Label(modelo_1, image=fn_1).place(x=0, y=0)
foto_1 = PhotoImage(file='mg_1.png')
nom_1 = Label(modelo_1, text ='Rose&Frites Hoodie con Gorra', fg ='black',
              font=('Arial',20,'bold')).place(x=375,y=50)
myfoto_1 = Label(modelo_1, image=foto_1).place(x=50, y=30)
text_1 = Label(modelo_1, text= 'Promoción!! Hoodie de color negro\n con una estampa en forma de rosa. '
                               '\nTejido principal: 65%'
                    ' algodón, 35% poliester.\nTejido secundario: 97% algodón, 3% elástico.'
                    '\n Gorra color amarillo con \n estampa de papas fritas.\n 97% algodón, 3% elástico'
                    '\n100% Hecho en Ecuador', bg='white', fg='black', font=('Arial', 16)).place(x=375, y=100)
tallas=Label(modelo_1,text="Tallas!",font=("Arial",16,'bold'),bg='white',fg='black').place(x=500,y=300)
xl = Button(modelo_1, text="XL",font=("Arial",9,"bold"),width=60,bg='black',fg='white').place(x=385,y=350)
l = Button(modelo_1, text="L",font=("Arial",9,"bold"),width=60,bg='black',fg='white').place(x=445,y=350)
m = Button(modelo_1, text="M",font=("Arial",9,"bold"),width=60,bg='black',fg='white').place(x=505,y=350)
s = Button(modelo_1, text='S',font=("Arial",9,'bold'),width=60,bg='black',fg='white').place(x=565,y=350)
xs = Button(modelo_1, text='XS',font=("Arial",9,'bold'),width=60,bg="black",fg="white").place(x=625,y=350)
precio = Label(modelo_1, text="$35.00", font=('Arial',15, 'bold')).place(x=50, y= 485)
cuidados=PhotoImage(file='cuidados.png')
cuidados1=Label(modelo_1,image=cuidados).place(x=375,y=400)
cuidados2=Label(modelo_1,text="Cuidados",font=("Arial",15,'bold')).place(x=500,y=505)
iva = Label(modelo_1,text='El precio no contiene I.V.A.', font=('Arial',14,'bold')).place(x=500,y=660)
back = Button(modelo_1, text='BACK!', font=('verdana', 14, 'bold'), fg='red',
              command= modelo_1.destroy).place(x=0, y=0)

【问题讨论】:

标签: python button tkinter back


【解决方案1】:

当您使用 tkmacosx Button 销毁任何窗口时,将触发错误 _tkinter.TclError: bad window path name 此问题已通过对 tkmacosx (0.1.3) 的最新更新得到修复。

通过像这样使用 pip 更新包应该可以修复错误。

pip install tkmacosx --upgrade

pip install tkmacosx==0.1.3

【讨论】:

    【解决方案2】:

    在您的代码中使用它:

    back = Button(modelo_1, text='BACK!', font=('verdana', 14, 'bold'), fg='red',
                  command= lambda:modelo_1.destroy).place(x=0, y=0)
    

    【讨论】:

    • 欢迎来到 StackOverflow。虽然这段代码可以解决问题,including an explanation 解决问题的方式和原因确实有助于提高帖子的质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的回答添加解释并说明适用的限制和假设。
    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多