graphical apps with tkinter

days_64

Python64

Posted on March 8, 2020

graphical apps with tkinter

You can use the tkinter module to create graphical applications and desktop applications with Python

For example, the Python desktop program below creates two buttons and a label. Tkinter has many widgets you can add to a window, labels and buttons being just one of them.

from tkinter import *

class OnOffButton(Button):
    def __init__(self,master=None,text=None):
        Button.__init__(self,master,text=text)
        self['command'] = self._onButtonClick

    def _onButtonClick(self):
        print('button clicked')


class App(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)

        self.labelHello = Label(self,text="Hello World")
        self.labelHello['fg'] = "red"
        self.labelHello.grid()

        self.button1 = OnOffButton(self,text="Click Me")
        self.button1.grid()

        self.button2 = OnOffButton(self,text="Click me?")
        self.button2.grid()

def main():
    root = Tk()
    app = App(master=root)
    app.grid()
    root.mainloop()

if __name__ == '__main__':
    main()

In this example there are 3 widgets: a label and a button.
You can create a label by calling

Label(self,text="Hello World")

To create a button, call (the class is subclassed)

Button(self,text="Click me?")

tkinter buttons and label

So you can make all kinds of desktop apps with tkinter.
But you can create "graphical apps", similar to Paint. (source: canvas demo)

draw with tkinter

These are just simple examples. You can create much more complex graphical user interfaces, as shown in the image below:

tkinter gui

More reading:

💖 💪 🙅 🚩
days_64
Python64

Posted on March 8, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Learning Python
javascript Learning Python

November 28, 2024

Calculate savings with Python!
beginners Calculate savings with Python!

November 26, 2024

UV the game-changer package manager
programming UV the game-changer package manager

November 24, 2024

Beginners Guide for Classes
python Beginners Guide for Classes

November 20, 2024