DEV Community

petercour
petercour

Posted on

1

Use Tkinter to make a Python GUI?

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()
Enter fullscreen mode Exit fullscreen mode

Related links:

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay