DEV Community

Cover image for graphical apps with tkinter
Python64
Python64

Posted on

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:

Top comments (2)

Collapse
 
muhimen123 profile image
Muhimen

Well, I prefer PyQt5 as it gives you a nice little handy software to make UI and then turn it into a code.

Collapse
 
sativaphoenix profile image
Darryn Dumisani Ph☻enix-92

O, i do love how there are so many varieties!