If I created Tkinter window with some text that filled the whole window and now wanted to replace the window with a new text, is there a way to refresh the window?
For Example:
a= 100
win= Tk()
win.geometry("500x300")
while a > 0:
if a%2 == 0:
lbl = Label (win, bg = "purple")
lbl.pack()
else:
lbl = Label (win, bg = "blue")
lbl.pack()
a= x-1
The problem with this code is that the Tkinter window does not refresh and just provides the end result instead of showing the windows changing colors.
Thanks for the help!
That is not the way to change UI states, because even if you refreshed the window it would be so quick you won't notice, instead change the state, wait some time and change the state again e.g. here I show how to animate color
from Tkinter import *
index = 0
def changeColor():
global index
if index%2==0:
label.configure(bg = "purple")
else:
label.configure(bg = "blue")
index+=1
label.after(1000, changeColor)
root = Tk()
mainContainer = Frame(root)
label = Label(mainContainer, text="")
label.configure(text="msg will change every sec")
label.pack(side=LEFT, ipadx=5, ipady=5)
mainContainer.pack()
label.after(1000, changeColor)
root.title("Timed event")
root.mainloop()
This Is How I Do To Update Data From Sql Server in tkinter GUI python3
from tkinter import *
import os
window=Tk()
window.geometry('300x300')
def update():
window.destroy()
os.system('test.py')
Button(window,text="Refresh",command=update)
window.mainloop()
Related
take a look at this code
well it happens to just add another label for no reason
import tkinter
from tkinter import *
clicks = 1
def click_count():
global clicks
# making the label that shows how many idk you have
label = Label(frame, text="you have " + str(clicks), font=(('Courrier'), 32))
label.pack()
clicks += 1
label.pack()
#generating the window
root = tkinter.Tk()
root.geometry('500x500')
#making the expandable frame
frame = Frame(root)
frame.pack(expand=YES)
#making the button
button = Button(frame, text= "click", font=(('Courrier'), 32), command=click_count)
button.pack()
root.mainloop()
and then i tried this
and i also tried to remove the label.pack at the end
but it still does the same thing which is adding another label
import tkinter
from tkinter import *
clicks = 1
def click_count():
global clicks
# making the label that shows how many idk you have
label = Label(frame, text="you have " + str(clicks), font=(('Courrier'), 32))
label.pack()
label.destroy()
clicks += 1
label.pack()
#generating the window
root = tkinter.Tk()
root.geometry('500x500')
#making the expandable frame
frame = Frame(root)
frame.pack(expand=YES)
#making the button
button = Button(frame, text= "click", font=(('Courrier'), 32), command=click_count)
button.pack()
root.mainloop()
i was expecting it to add a nmber to the label but it just shows another label
It does not add a Label for no reason. It adds the label because that's what your function tells it to do. Each time you click the button, the function is executed that creates and packs a new Label.
What you should do is create the label at the onset and link it to a variable. Then, you can change the value of this variable in the function.
Also, you don't have to import tkinter twice and it's sensible to update the clicker first and then display the result instead of showing the last value it had. Your approach works in such a small program but the value of clicks will always be one higher than displayed. So, you may get into problems when you use the value.
from tkinter import *
def click_count():
global clicks
clicks += 1
click_counter.set("you have " + str(clicks))
#Initiate root
root = Tk()
root.geometry('500x500')
#Set initial values for click counter
clicks = 0
click_counter = StringVar()
click_counter.set("you have 0")
#making the expandable frame
frame = Frame(root)
frame.pack(expand=YES)
# making the label that shows how many idk you have
label = Label(frame, textvariable=click_counter, font=(('Courrier'), 32)) ## The label gets updated, whenever the value of click_counter changes.
label.pack()
#making the button
button = Button(frame, text= "click", font=(('Courrier'), 32), command=click_count)
button.pack()
root.mainloop()
all I need in tkinter help, cos when you click with mouse three times read as once in counter. I know about it. I tried a couple of methods in mouse-event and I try with My current code. Tnx for your help in advance
from tkinter import *
counter=0
def mouse_click(event):
global counter
print('Counter is ',counter)
counter+=1
window = Tk()
window.minsize(300, 100)
label = Label( window, text="Click here")
label.pack()
label.bind( "<Double-Button>", mouse_click)
window.mainloop()
I have found a solution to this code thanks to everyone who tried
from tkinter import *
counter = 0
def mouse_click_times(event):
global counter
print("You press triple button times: ",counter)
counter +=1
windows = Tk()
windows.minsize(400,250)
label = Label(windows, text = "Click me 3x: ",bg = "blue", fg = "white")
label.pack()
label.bind("<Triple-Button-1>",mouse_click_times)
windows["bg"]="green"
exit_from_tkinter_loop = Button(windows, text="Exit from window", bg =
"blue", fg = "white",command=windows.destroy)
exit_from_tkinter_loop.pack(pady = 20)
windows.mainloop()
The event Double-Button is fired for double clicks. If you want to count individual clicks, use Button
from tkinter import *
counter = 0
def mouse_click(event):
global counter
print('Counter is ', counter)
counter += 1
window = Tk()
window.minsize(300, 100)
label = Label(window, text="Click here")
label.pack()
label.bind("<Button>", mouse_click)
window.mainloop()
I am trying to create a tkinter application where every time you press a button an image moves down a certain number of pixels. For example, the first time it is placed at y=30 and then the next time the button is pressed it is placed at y=60 etc. Is there any way to do this? I do not want to use the pack() method as I need to place the image in a specific location on the screen using x and y coordinates.
import calendar
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
def display():
box_image = tk.PhotoImage(file='apple.png')
panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
panel2.image = box_image
panel2.place(x=30, y=30 + 30) #i was thinking about doing something like adding 30 each time but this didn't work
button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)
root.mainloop()
do the following :
import calendar
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry('800x800')
x = 30
y = 30
box_image = tk.PhotoImage(file=r'apple.png')
def display():
global x , y
panel2 = tk.Label(root, image=box_image, bg='#f7f6f6')
panel2.place(x=x+30, y=y+30)
x = x+30
y = y+30
button = tk.Button(root, text="click me", command=display)
button.place(x=0, y=0)
root.mainloop()
Is it possible to get the popup option (pop function in the code) on hovering the cursor on the button?
import tkinter as tk
from tkinter import Tk, BOTH, Menu
def pop(bt):
try:
x = bt.winfo_rootx()+238
y = bt.winfo_rooty()+10
popup.tk_popup(x, y, 0)
finally:
popup.grab_release()
root = tk.Tk()
popup = Menu(root, tearoff=0,relief='raised')
popup.add_command(label="About")
popup.add_command(label="User manual")
popup.add_command(label="Contact us")
button1 =tk.Button(root, text="HELP",height=3,width=26,command=lambda: controller.show_frame(HelpPage))
button1.configure(command = lambda: pop(button1))
button1.place(x=0,y=0
)
root.mainloop()
.
button1.bind('<Enter>',pop(button1)) #gives the following output without the mouse cursor over that button.
Try this:
import tkinter as tk
window = None
def leave_window(event):
global window
if 0 < root.winfo_pointerx() - root.winfo_rootx() < button.winfo_width():
if 0 < root.winfo_pointery() - root.winfo_rooty() < button.winfo_height():
# Mouse still over button
return None
if window is not None:
window.destroy()
window = None
def create_window(event):
global window
if window is not None:
# The window is already open
return None
window = tk.Toplevel(root)
window.overrideredirect(True)
label = tk.Label(window, text="Text", bg="black", fg="white")
label.pack()
window.update()
# Move the window to the cursor's
x = root.winfo_pointerx()
y = root.winfo_pointery()-window.winfo_height()
window.geometry("+%i+%i" % (x, y))
window.bind("<Leave>", leave_window)
root = tk.Tk()
button = tk.Button(root, text="-------- Hover the mouse here --------")
button.pack(fill="both", expand=True)
button.bind("<Enter>", create_window)
button.bind("<Leave>", leave_window)
root.mainloop()
I binded to <Enter> and <Leave> to check if the mouse is over the button. If it is, it creates a window with a label called text. You can change it to it showing the menu.
For more answers look here.
Please bind your Button with mouse enter and call pop function.
Check out this code :
button1.bind('<Enter>', pop)
Control your popup location as well as destroy popup on '' case again bind it and call custom function that will destroy popup.
You need <Enter> and <Leave> bind sequences to map and unmap the Menu. Tkinter Menu has two methods post and unpost where post shows the menu at given coordinates, and unpost hides it away. Unfortunately, I couldn't test it as the unpost functionality doesn't work on macOS or Linux [refer to this link for the same]. I also changed the x, y coords to map the Menu in the center of the widget (Button), it can be changed if required.
Here is the complete sample code.
import tkinter as tk
from tkinter import Tk, BOTH, Menu
def pop(evt):
but = evt.widget
if str(evt.type) == "Enter":
# Map the menu in the center of the width.
x = but.winfo_rootx() + int(but.winfo_width()/2)
y = but.winfo_rooty() + int(but.winfo_height()/2)
popup.tk_popup(x, y)
elif str(evt.type) == "Leave":
popup.unpost()
root = tk.Tk()
root.geometry("300x300")
popup = Menu(root, tearoff=0, relief='raised')
popup.add_command(label="About")
popup.add_command(label="User manual")
popup.add_command(label="Contact us")
button1 = tk.Button(root, text="HELP", height=3, width=26)
button1.bind('<Enter>', pop)
button1.bind('<Leave>', pop)
button1.pack(pady=100)
root.mainloop()
Like said, unpost doesn't work on macOS or Linux, so I couldn't test the sample code 100% but it should work fine.
I am looking to move a tkinter window to different x and y coordinates after it has been created. For example 5 seconds after the window has been created move it to
x=??? and y=???.
from tkinter import*
root = Tk()
root.title("lol")
root.geometry("300x300")
photo = PhotoImage(file="pepe.gif")
label = Label(image=photo).place(x=10, y=10)
root.mainloop()
The above code is just an example, how does it need to be modified to move by itself?
This example will show you how to reset the root geometry that controls the position of the window on screen; clicking on the image will toggle the locationof the window.
from tkinter import *
def move_me(idx=[0]): # <- creates a closure for the index of the location strings
loc = ["300x300+200+200", "300x300+300+300"]
root.geometry(loc[idx[0]]) # <-- reset the geometry
idx[0] = (idx[0]+1) % 2 # <-- toggles the index between 0 and 1
root = Tk()
root.title("lol")
root.geometry("300x300-100+100")
photo = PhotoImage(file="pepe.gif")
button = Button(root, image=photo, command=move_me)
button.place(x=10, y=10)
root.mainloop()
[edit] with auto repeat moving the window.
(attention, you have challenging game to catch the window to close it)
from tkinter import *
import random
def move_me():
x, y = str(random.randrange(800)), str(random.randrange(800))
loc = "300x300+" + x + '+' + y
root.geometry(loc)
root.after(500, move_me) # <-- auto call to move_me again every 1/2 second
root = Tk()
root.title("lol")
root.geometry("300x300+100+100")
photo = PhotoImage(file="pepe.gif")
label = Label(root, image=photo)
label.place(x=10, y=10)
move_me() # <-- call to move_me
root.mainloop()