Python Forum
tkinter.TclError: bad window path name "!button"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter.TclError: bad window path name "!button"
#1
Bug 
So, I was searching for a way to ask my girl out and I found this really cool code on Instagram and copied it. But it just doesnt work correctly, the "no" window should change to a random place every time the person try to click on, but instead it only disappears. Plsss help, im really looking forward to ask this girl out. (p.s. im sorry for all the errors, im pretty much new to forums so I dont understand yet how it works
The error I get is this:

Error:
Exception in Tkinter callback Traceback (most recent call last): File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/tkinter/__init__.py", line 1921, in __call__ return self.func(*args) File "main.py", line 12, in move_button_1 if abs(e.x - button_1.winfo_x()) < 50 and abs(e.y - button_1.winfo_y()) < 40: File "/nix/store/hd4cc9rh83j291r5539hkf6qd8lgiikb-python3-3.10.8/lib/python3.10/tkinter/__init__.py", line 1333, in winfo_x self.tk.call('winfo', 'x', self._w)) _tkinter.TclError: bad window path name ".!button"
import tkinter as tk
from tkinter import *
import random
from tkinter import messagebox

root = tk.Tk()
root.title('Aceitas?')
root.geometry('600x600')
root.configure(background='#ffc8dd')

def move_button_1(e):
  if abs(e.x - button_1.winfo_x()) < 50 and abs(e.y - button_1.winfo_y()) < 40:
    x = random.randint(0, root.winfo_width() - button_1.winfo_width())
    y = random.randint(0, root.winfo_height() - button_1.winfo_height())
    button_1.place(x=x, y=y)

def accepted():
  messagebox.showinfo(
    'Meu amor', 'Eu te amo meu amor, lanchinho mais tarde?')

def denied():
  button_1.destroy()

margin = Canvas(root, width=500, bg='#ffc8dd', height=100,
                bd=0, highlightthickness=0, relief='ridge')
margin.pack()
text_id = Label(root, bg='#ffc8dd',text='Quer namorar comigo?',
                fg='#590d22', font=('Montserrat', 24, 'bold'))
text_id.pack()
button_1 = tk.Button(root, text='Não', bg='#ffb3c1', command=denied,
                     relief=RIDGE, bd=3, font=('Montserrat', 8, 'bold'))
button_1.pack()
root.bind('<Motion>', move_button_1)
button_2 = tk.Button(root, text='Sim', bg='#ffb3c1', relief=RIDGE,
                    command=accepted, bd=3, font=('Montserrat', 14, 'bold'))
button_2.pack()

root.mainloop()
Reply
#2
denied() destroys the button. Maybe you want pressing button_1 to call move_button_1()?
import tkinter as tk
import random
 
root = tk.Tk()
root.geometry("600x600")
 
def move_button_1():
    button_1.place(
        x=random.randint(0, root.winfo_width() - button_1.winfo_width()),
        y= random.randint(0, root.winfo_height() - button_1.winfo_height())
    )

button_1 = tk.Button(root, text='Não', command=move_button_1)
button_1.pack()

root.mainloop()
Reply
#3
It worked, thanks!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  add entries and labels to the window tkinter jacksfrustration 3 634 Oct-10-2023, 06:41 PM
Last Post: buran
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,799 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  how to open a popup window in tkinter with entry,label and button lunacy90 1 898 Sep-01-2023, 12:07 AM
Last Post: lunacy90
  Tkinter button images not showing up lunacy90 7 1,614 Aug-31-2023, 06:39 PM
Last Post: deanhystad
  tkinter.TclError foxxrunning 10 1,593 Jul-05-2023, 01:48 AM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,185 Jun-13-2022, 08:59 AM
Last Post: Shena76
Sad Selenium: can't open a new window after clicking a button and I don't know why noahverner1995 0 1,990 Jan-22-2022, 09:55 AM
Last Post: noahverner1995
  Closing Threads and the chrome window it spawned from Tkinter close button law 0 1,714 Jan-08-2022, 12:13 PM
Last Post: law
  tkinter auto press button kucingkembar 2 3,191 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,224 Sep-09-2021, 01:25 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020