School assignment Tkinter with method triple button - python

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()

Related

Tkinter label does not change images dynamically

I want to change the image of the one labels in time with 2 other images but it immideatly skipts to the last one. Any suggestions?
I have tried using time.sleep incase it might happen so fast that before i could notice but it didnt work.
import tkinter
from tkinter import *
from PIL import Image , ImageTk
import time
window = tkinter.Tk()
window.geometry("500x500")
window.title("Pomodoro Timer")
window.image_1 = ImageTk.PhotoImage(Image.open("1.jpg"))
window.image_2 = ImageTk.PhotoImage(Image.open("2.jpg"))
window.image_3 = ImageTk.PhotoImage(Image.open("3.jpg"))
lbl_1 = tkinter.Label(window, image=window.image_1)
lbl_1.place(x=150,y=100)
lbl_2 = tkinter.Label(window, image=window.image_2)
lbl_2.place(x=200,y=100)
def display_numbers():
lbl_1
lbl_2
display_numbers()
def clicked():
i=1
while i<3:
if i==1:
lbl_1.configure(image=window.image_2)
time.sleep(0.91)
i += 1
elif i==2:
lbl_1.configure(image=window.image_3)
time.sleep(0.91)
i += 1
btn = tkinter.Button(window, text="Start", command=clicked)
btn.place(x=200,y=450,width=100)
window.mainloop()

Button click counter in Python

I am trying to write a Python program to count how many times a button is clicked. I have written the following code:
import tkinter
from tkinter import ttk
def clicked(event):
event.x = event.x + 1
label1.configure(text=f'Button was clicked {event.x} times!!!')
windows = tkinter.Tk()
windows.title("My Application")
label = tkinter.Label(windows, text="Hello World")
label.grid(column=0, row=0)
label1 = tkinter.Label(windows)
label1.grid(column=0, row=1)
custom_button = tkinter.ttk.Button(windows, text="Click on me")
custom_button.bind("<Button-1>", clicked)
custom_button.grid(column=1, row=0)
windows.mainloop()
I know that event.x is used to capture the location of mouse. And hence the result of the program is not as expected. I want something else. Can you please help me solve the issue.
You don't need event for this. You need own variable to count it.
And it has to be global variable so it will keep value outside function.
count = 0
def clicked(event):
global count # inform funtion to use external variable `count`
count = count + 1
label1.configure(text=f'Button was clicked {count} times!!!')
EDIT: Because you don't need event so you can also use command= instead of bind
import tkinter as tk
from tkinter import ttk
count = 0
def clicked(): # without event because I use `command=` instead of `bind`
global count
count = count + 1
label1.configure(text=f'Button was clicked {count} times!!!')
windows = tk.Tk()
windows.title("My Application")
label = tk.Label(windows, text="Hello World")
label.grid(column=0, row=0)
label1 = tk.Label(windows)
label1.grid(column=0, row=1)
custom_button = ttk.Button(windows, text="Click on me", command=clicked)
custom_button.grid(column=1, row=0)
windows.mainloop()
You can add 'counter' as an attribute with a decorator like this:
def static_vars(**kwargs):
def decorate(func):
for k in kwargs:
setattr(func, k, kwargs[k])
return func
return decorate
#static_vars(counter=0)
def clicked(event):
clicked.counter += 1
I don't know is this best way or not, but this code is worked
import tkinter as tk
class Main():
def __init__(self, root):
self.root = root
self.count = 0
frame = tk.Frame(self.root)
frame.pack()
btn = tk.Button(frame, text ='click me')
btn.pack()
btn.bind('<Button-1>', self.click)
self.lbl = tk.Label(frame, text = 'Count is 0')
self.lbl.pack()
def click(self, event):
self.count += 1
self.lbl.config(text=f'count {self.count}')
if __name__=="__main__":
root = tk.Tk()
Main(root)
root.mainloop()

tkinter 'var' for radiobutton is always returning 0

from the List of radio button I want to know which one was clicked
Whenever a radio button (In python Tkinter) is clicked its returning 0...
I tried the following method:
declaring the 'var' variable global
passing var variable in all function
But none of the steps are working
def get_date(var):
path_read = E1.get()
date_list = readunparseddata.getdate_unparseddate(path_read)
show_date(date_list,var)
def show_date(list_date,var):
print(var)
frame = Tk()
#v.set(1)
Label(frame,text="""Choose your Date :""",justify=LEFT,padx=20).pack( anchor = W )
count = 0
for date in list_date:
print count
R1=Radiobutton(frame, text=date, padx=20, value=count, variable=var, command=lambda:ShowChoice(var))
R1.pack()
count+=1
def ShowChoice(var):
print "option : " + str(var.get())
top = Tk()
var=IntVar()
The problem was with the instance of Tk() that I was creating.
Below link ( 1 ) said to use TopLevel() which solved the problem
Increment the counter in the function that is being called when the radio button is selected.
Here is an example to help you.It prints the number of times the button is selected.
import Tkinter as tk
count=0
root = tk.Tk()
def add():
global count
count=count+1
print count
v = tk.IntVar()
tk.Label(root,
text="""Choose a
programming language:""",
justify = tk.LEFT,
padx = 20).pack()
tk.Radiobutton(root,
text="Python",
padx = 20,
variable=v,
value=1,command=add).pack(anchor=tk.W)
root.mainloop()

Tkinter Label text is Variable

A hoy hoy, I would like to know how I can take a parameter in a function and make it a label and also then .grid_forget() label again (Answer is on a "Restart?" window). I'm making a currency converter from scratch and my code is terrible.
Take a look at one of my Tkinter programs. Look at where it says counter. That is the variable. The Tkinter website is also so helpful! Good luck!
from tkinter import *
import sys
root = Tk()
root.geometry("480x320")
root.title("School Days Left!")
global counter
counter = 30
def upClick():
global counter
counter += 1
def downClick():
global counter
counter -= 1
mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")
mButton1 = Button(text = counter, command = downClick, height = 4000, width = 320, font = ("Monospace", 200))
mButton1.pack()
root.mainloop()

How do you refresh a window in Tkinter

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()

Categories