Okay, I made a little tkinter program that basically counts how many times the button was clicked and implemented the triple button module so that it counts every third click, but I wanted to change it up so that the button instead of saying "Click me!", displays the number of clicks itself.
import tkinter as tk
class Main():
def __init__(self, root):
self.root = root
self.count = 0
frame = tk.Frame(self.root)
frame.pack()
label = tk.Label(root, text="Super click counter!", font=('Arial', 18))
label.pack(padx=20, pady=20)
btn = tk.Button(frame, text ='Click me!')
btn.pack()
btn.bind('<Triple-Button>', self.click)
self.label = tk.Label(frame, text = 'The button was clicked 0 times.')
self.label.pack()
def click(self, event):
self.count = 1
self.label.config(text=f'The button was clicked {self.count} times.')
if __name__=="__main__":
root = tk.Tk()
root.geometry("300x150")
root.title("A serious video game.")
Main(root)
root.mainloop()
CodePudding user response:
we add the btn
property by using self.btn =
then we change the text inside it when the button is clicked 3 times using self.btn.config(text=self.count.__str__())
import tkinter as tk
class Main():
def __init__(self, root):
self.root = root
self.count = 0
frame = tk.Frame(self.root)
frame.pack()
label = tk.Label(root, text="Super click counter!", font=('Arial', 18))
label.pack(padx=20, pady=20)
self.btn = tk.Button(frame, text ='Click me!')
self.btn.pack()
self.btn.bind('<Triple-Button>', self.click)
self.label = tk.Label(frame, text = 'The button was clicked 0 times.')
self.label.pack()
def click(self, event):
self.count = 1
self.label.config(text=f'The button was clicked {self.count} times.')
self.btn.config(text=self.count.__str__())