I was trying to make a simple GUI based game that has a button having the text CLICK ME .Whenever the user clicks on the button the total number clicks are displayed on the button.
Here is my code
from Tkinter import *
class Application(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.grid()
self.bttn_click = 0
self.create_widget()
def create_widget(self):
self.bttn = Button(self)
self.bttn["text"] = "Total Clicks = 0"
self.bttn["command"] = self.update_count()
self.bttn.grid()
def update_count(self):
self.bttn_click += 1
self.bttn["text"] = "Total Clicks = " + str(self.bttn_click)
#main
root = Tk()
root.geometry("900x700")
root.title("Click Counter")
app = Application(root)
root.mainloop()
Please read it from the official documentation . In there, the first "Hello World" Example has almost the same code as you do.
The command self.bttn["command"] = self.update_count()
assigns the return value of def update_count(self)
to your button command.
If you think to know what the result of some action should be, you could always use a print statement afterwards to verify what your assignment did.
self.bttn["command"] = self.update_count()
print(self.bttn["command"])
What exactly is the issue and where does it come from?
in the code line of yours mentioned above, you are immediately calling self.update_count
and not assigning the function to be called everytime the button is pressed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.