DEV Community

loading...

Use Tkinter to make a Python GUI?

petercour
・1 min read

There are many GUI modules for Python. One of them is Tkinter. So what about Tkinter?

Maybe. If your program doesn't need complex GUI widgets or a native GUI.

If you need many widgets and native operating system look, it may be better to go with the PyQt5 module.

Tkinter has been around for a long long time. It contains the basic widgets

  • Button, Checkbutton, Menubutton, Radiobutton
  • Canvas
  • Entry
  • Frame
  • Label, LabelFrame
  • Listbox
  • Menu ... and so on.

So you can build basic GUI interfaces. But it doesn't have an enormous list of widgets.

On the other hand, it has an easy learning curve. The hello world program looks like this:

#!/usr/bin/python3
import tkinter as tk

class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.pack()
        self.create_widgets()

    def create_widgets(self):
        self.hi_there = tk.Button(self)
        self.hi_there["text"] = "Hello World\n(click me)"
        self.hi_there["command"] = self.say_hi
        self.hi_there.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                          command=self.master.destroy)
        self.quit.pack(side="bottom")

    def say_hi(self):
        print("hi there, everyone!")

root = tk.Tk()
app = Application(master=root)
app.mainloop()

Related links:

Discussion (0)