My program should be a very simple counter, but yet m not able to figure why it is not counting when the button clicked.
from tkinter import *
class Counter:
def __init__(self):
self.__value = 0
self.__main_window = Tk()
self.__current_value = Label(self.__main_window, text=self.__value)
self.__current_value.pack()
self.__increase_button = Button(self.__main_window, text='Increase',
command=self.increase)
def increase(self):
self.__value += 1
def main():
Counter()
if __name__ == "__main__":
main()
The text configuration of a Label in tkinter is not auto-updating. The value stored in the self.__value
variable is evaluated and displayed as the label.
Subsequent changes to the value of self.__value
will not be reflected in the GUI.
When you update the self.__value
variable you will also want to reconfigure the self.__current_value
label to reflect these changes. You can update your increase
method to reconfigure the label like so
def increase(self):
self.__value += 1
self.__current_value.config(text=self.__value)