Click here to Skip to main content
14,865,158 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
from tkinter import *

root = Tk()
root.title("Title")
root.geometry("500x500")
root.configure(bg='black')
def MyClick():
    MyLabel = Label(root, text = "I clicked!", fg= "red", bg="black")
    MyLabel.pack()
My_Button = Button(root, text = "Click", command = MyClick, padx = 50)
My_Button.pack()
root.mainloop()


What I have tried:

When i click "Click" the output it will be this: Imgur: The magic of the Internet[^]
I want every 3 times i press click, the text to be shown like +1.
The output is "I clicked!" . So if i press 3 times "Click", to show me "I clicked! I clicked!", after 3 times "I clicked! I clicked! I clicked!" and so on... how can i do that ?
Posted
Updated 1-Jan-21 8:49am

Add one to a counter. Take the modulus using 3. if the result is zero, there has been a multiple of 3 clicks.

Python Language - Modulus | python Tutorial[^]
   
Comments
Vlad Țepeș ツ 24-Dec-20 14:40pm
   
I dont understand, i just begin learning python and i'm not so advantage, can u write the code here?
from tkinter import *
count = 0
labels = []
root = Tk()
root.title("Title")
root.geometry("500x500")
root.configure(bg='black')
def MyClick():
	global count
	global labels
	count+=1
	c = 0
	for l in labels:
		l.destroy()
	while(c<count):
		MyLabel = Label(root, text = "I clicked!", fg= "red", bg="black")
		MyLabel.pack()
		labels.append(MyLabel)
		c+=1
	

My_Button = Button(root, text = "Click", command = MyClick, padx = 50)
My_Button.pack()
root.mainloop()


Try the code above, I added two global variables and two loops to the 'MyClick' function, first 'count' which keeps the record for clicks and shows label that many times and second list variable 'labels' which will have all the labels inside it and it will destroy all the labels inside it before showing new ones (kinda like clearing the screen). If you have any doubt, feel free to ask.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900