3
\$\begingroup\$

This code was to create how many people enter the "building" so there will be someone clicking the button "count". I am still improving. Please let me know your honest opinions!

from tkinter import *
import math

win = Tk()

global counter
counter = 0

def closewindow():
    exit()



def continuewindow():
    global counter
    counter+=1
    print ("Total number of people entering: ")
    print (counter)


b1 = Button(win,text="Count",command=continuewindow)
b2= Button(win,text="Exit",command=closewindow)


b1.pack(side=LEFT)
b2.pack(side=LEFT)
mainloop()
\$\endgroup\$
4
\$\begingroup\$

Personally, when building GUI applications I like to wrap my code into a class, and handle each action into each own method. I'll start from top to bottom.

I don't use wildcard imports. I import the package as "tk", which requires that I prefix all my tkinter commands with tk. This prevents global namespace pollution, plus it makes the code completely obvious when you are using tkinter classes, ttk classes, or some of your own.

So, I'd write the import as:

import tkinter as tk

The main application should be a class. This will give you a private namespace for all your callbacks and private functions (if any), and just generally makes it easier to organize the code. The way you did (procedural style) you have to code top-down, defining functions before using them, etc. With this method you don't, since you don't actually create the main window until the very last step. (as you'll see).

Let's start building our class step by step:

class CountVisitors:
    """
    GUI Application which counts how many visitors enter the building.
    The application prints the count of visitors in the console
    """

I've started by giving our class a name, and writing just below it a docstring which tells the users what the class is supposed to do.

After we've defined our class, we should initialize some of the components of our application and we're gonna use a constructor, which by convention is the __init__ method:

def __init__(self, master):
    self.master = master

    self.button1 = tk.Button(self.master, text="Count", command=self.count_visitors)
    self.button2 = tk.Button(self.master, text="Exit", command=self.close_window)

    self.button1.pack(side=tk.LEFT)
    self.button2.pack(side=tk.LEFT)

    self.button_clicks = 0

Now, whether or not __init__ is a constructor or not is not our concern here. If you're not familiar with what a constructor it is, well...it's nothing more than a special kind of method that Python calls when it instantiates an object using the definitions found in some class. Basically, Python relies on the constructor to perform tasks such as initializing (assigning values to) any instance variables that the object will need when it starts.

So, in the above code, we've initialized our parent app, our buttons, and the counter that will keep track of the visitors.

Moving forward, I'll create two separate methods which will handle the two actions we'll have in our app:

  • counting / printing the visitors
  • closing the application
def count_visitors(self):
    self.button_clicks += 1
    print(self.button_clicks)

def close_window(self):
    self.master.destroy()

If you didn't noticed, it wasn't necessary to force the application to close using exit() (which by the way, is frowned upon) because tkinter has its own way of doing exactly that using the destroy() method.

Now, all you have to do is to create a main() method where you'll instantiate the class and start it:

def main():
    root = tk.Tk()
    CountVisitors(root)
    root.mainloop()

The entire code:

import tkinter as tk


class CountVisitors:
    """
    GUI Application which counts how many visitors enter the building.
    The application prints the count of visitors in the console
    """
    def __init__(self, master):
        self.master = master

        self.button1 = tk.Button(self.master, text="Count", command=self.count_visitors)
        self.button2 = tk.Button(self.master, text="Exit", command=self.close_window)

        self.button1.pack(side=tk.LEFT)
        self.button2.pack(side=tk.LEFT)

        self.button_clicks = 0

    def count_visitors(self):
        self.button_clicks += 1
        print(self.button_clicks)

    def close_window(self):
        self.master.destroy()


def main():
    root = tk.Tk()
    CountVisitors(root)
    root.mainloop()

if __name__ == '__main__':
    main()
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.