Home > Mobile >  Move tkinter button randomly on mouse hover
Move tkinter button randomly on mouse hover

Time:03-24

I'm trying to make a button move randomly to coordinates in lists. It moves the first time I hover on it each time I run the programme and never after and I can't figure it out, I'm a total noob, though and frankly I'm amazed it works at all. I hate asking questions but I've been trying to solve this for weeks so I appreciate anyone's help..

import tkinter as tk
from tkinter import *
import random

root = tk.Tk()
root.title('Press me')
root.geometry('1280x800')

img=PhotoImage(file='red_button_crop.png')
my_label=Label(root, text="",font='helvetica,12')
my_label.pack(pady=50)  

but_loc_x=[200,600,1000]
but_loc_y=[200,350,600]
but_pos_x=random.choice(but_loc_x)
but_pos_y=random.choice(but_loc_y)
def press():
    my_label.config(text="You pressed me.\n Well done\nYou are as clever as a monkey.\n Not one of the good ones tho")   
    root.after(1500,root.destroy)
    
def button_hover(e):
    root.after(250)
    button.place_configure(x=but_pos_x,y=but_pos_y)
    
#def unhover(e):
   # root.after(500)
    #button.place_configure(x=600,y=350)
    

    
button =tk.Button(root,image=img,borderwidth=0,command=press)
button.place(x=600,y=350,anchor=CENTER)

button.bind("<Enter>",button_hover)
#button.bind("<Leave>",unhover)





root.mainloop()

I've tried implementing various bit of code I searched up on the net but I don't know enough to work it out

CodePudding user response:

Importantly, as one of the comments suggests, the button is moving every time you hover over it, so your code was in fact correct.

However, because the coordinates are set outside of the function it always hovers to the same place (so it appears like it was not moving).

The changes that correct to code to the desired output are below.

  1. removed from tkinter import * and used the tk alias as it should be.
  2. moved the random button positions into the function (see comment with arrow).

This is the corrected version of the code:

import tkinter as tk
# from tkinter import *
import random

root = tk.Tk()
root.title('Press me')
root.geometry('1280x800')

img=tk.PhotoImage(file='red_button_crop.png')
my_label=tk.Label(root, text="",font='helvetica,12')
my_label.pack(pady=50)  

but_loc_x=[200,600,1000]
but_loc_y=[200,350,600]
def press():
    my_label.config(text="You pressed me.\n Well done\nYou are as clever as a monkey.\n Not one of the good ones tho")   
    root.after(1500,root.destroy)
    
def button_hover(e):
    root.after(250)
    but_pos_x=random.choice(but_loc_x)   # <----- moved into the function
    but_pos_y=random.choice(but_loc_y)   # <----- moved into the function

    button.place_configure(x=but_pos_x,y=but_pos_y)
    
#def unhover(e):
   # root.after(500)
    #button.place_configure(x=600,y=350)
    

    
button =tk.Button(root,image=img,borderwidth=0,command=press)
button.place(x=600,y=350,anchor=tk.CENTER)

button.bind("<Enter>",button_hover)
#button.bind("<Leave>",unhover)

root.mainloop()

Result: the image red_button_crop.png now moves randomly when hovered over.

  • Related