Python Class


I am to encapsulate the GUI part of the code below into a Class.

from tkinter import *
from tkinter.ttk import *

def plus_one():
    """Increments counter by 1 """
    global click_counter, counter_label

    click_counter += 1
    counter_label["text"] = str(click_counter)

def minus_one():
    """Reduces counter value by 1"""
    global click_counter, counter_label

    click_counter -= 1
    counter_label["text"] = str(click_counter)

def main():
    global click_counter, counter_label
    click_counter = 0
    window = Tk()

    counter_label = Label(window, text=str(click_counter))
    counter_label.grid(row=0, column=0)

    plus_one_button = Button(window, text="+1", command=plus_one)
    plus_one_button.grid(row=2, column=0)
    minus_one_button = Button(window, text="-1", command=minus_one)
    minus_one_button.grid(row=2, column=1)



The program template is as follows:

from tkinter import *
from tkinter.ttk import *

# The Class code comes here

def main():
    """Set up the GUI and run it"""
    window = Tk()
    counter_gui = CounterGui(window)


Can you please help me with the code. I am a beginner, and appreciate any help.


Here are some helpfull links that explain classes in python. I hope it helps. classes (recommended)


Actually, I have done some work. I wrote the program code I provided as my solution to the problem:

I need to create a program that has two buttons and a label that displays the current value of a counter. One button, bearing the text +1, should add one to the counter, while the other button, labelled -1, should subtract one from it (there is no minimum and maximum value). The counter should start at zero.

When designing the functions for each button, we need to get the current value, change the value depending on the button press, then set the new value.

Hint: as in the button example above you will need two global variables: one for the current count and one for the label widget.

But I am hopelessly stuck in coding the class


apology’s for the accusation.

Have you checked out the links tho ? they should still be valid to answer your question.


Yes, the link was helpful. But my brain seems fried. Can you help me with the code please?


I rather not. The implementation is very simple but the understanding of classes is a bit harder.

If i would help you with the code there’s a big change you still would not know how these classes work. I advice sleeping a night over it, let it sink in and try again.

You could also start the python course that is available on this website. There is a whole section about classes in python.


I am facing a time crunch. My deadline for this problem ends in the next 70 minutes. If you help with the code, I would really appreciate it. I will then gladly spend the needed time to understand classes better.


I stand by my point. This forum is for people that are trying to learn how to code. This means that knowledge is more important than working code.

If you just want your code get fixed without knowing what is happening in the first place you should hire a freelancer or something that can help you out.