Python Forum

Full Version: tkinter.TclError: bad window path name "!button"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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()
It worked, thanks!!!