Home Blog Archive Internet Python: GUI programming - that you need to know

Python: GUI programming - that you need to know

  • Sep 12, 2025
  • 716
  • 0

Python programs look much better if you are running with a GUI (Graphical User Interface). Otherwise, it is used to often the Terminal. Here, we show you how to use the TkInter module in Python itself is a GUI program.

GUI programming with Python: window with TkInter

Before you get started with your GUI correctly, you must first import the TkInter module and create a window.
  • To import the module, add at the very beginning of your code the command "from tkinter import *" and the command "from tkinter import ttk" (without the quotation marks).
  • Now you can create with the command "root = Tk()" and a new window. Instead of the word "root" you can also use a different word. However, it is recommended, however, to use each program the same Variable, so you can orientate easier in the programming.
  • The name of your window, you can use the command "root.title("Test")" (without the quotation marks at the beginning and end). In this case, the window will have the name "Test".
  • At the end of your program, you just have to type the command "root.mainloop()" insert, so that the program is complete.
Python: window with TkInter

TkInter with Python: menu bar program

In almost every program has a menu you can today, bar find. Also, this allows you to easily program with the TkInter module.
  • It is important that you have already performed all the above steps. The Code for the menu bar, you need to "root.title("Test")" and "root.mainloop()" insert.
  • The programming of the menu bar with the command "menubar = Menu(root)" (without the quotation marks) to start.
  • A first menu item you can create with the command "FileMenu = Menu(menubar, tearoff=0)". Instead of the variable "file menu" you can choose, however, a separate Variable.
  • A first sub-point, you add the command "file menu.add_command(label="Save", command=save)". In this case, the point is called "Save". If you press this, would be to run the command "save ()". It is important that you have defined the commands before the "root = Tk ()"command in the Code.
  • Now you can add as many sub-points as you wish. Tip: With the command "file menu.add_separator()" allows you to add by the way, a dividing line between the individual points.
  • If you are using a menu item done, do not forget the command "menubar.add_cascade(label="file", menu=filemenu)" to add. This command adds the menu item named "file".
  • If you want to add more menu items, you need to not only change the Variable of the menu item so that it comes to errors. Instead of "file menu" you could use each time, for example, "edit menu".
  • Do not forget, at the very end of your code, before the command "root.mainloop()" the command "root.config(menu=menubar)" insert, so that your menu bar is also displayed.
TkInter: menu bar program

GUI with Python: Labels and Entries, add

Now you can start to text, and text fields to your Python program, add.
  • A first Label can you add the command "a = Label (text="Name: ", bg="white", fg="black")" (without the quotation marks). This Label is represented by the Variable "a" and has the Text "Name: ". The font color of the text is black and the Background white.
  • Thus, the Label is displayed, you need to write including the command "a. grid ()". What you need grid (), you will learn in the next paragraph.
  • A text box you can create with the command "b = Entry ()". In this case, the text field is represented by the Variable "b".
  • Here, too, you should be back to the grid command grid: "b()"
Python: Labels and Entries

TkInter programming: Grid and Pack geometry managers

Surely you have asked yourself what you need the grid command:
  • This command is a geometry Manager. If the brackets are empty, does nothing.
  • However, you can change your command to: "b. grid(row=1, column=1, sticky=W)". The text field would now be located in row 1 and in column 1, and to the West (left edge of the screen) to be aligned.
  • With the grid command you can define the Position and orientation of individual objects. Note, however, that this command line 0 is the first row, and column 0 is the first column.
  • In addition to the grid geometry Manager the pack geometry Manager. An example of the command "b. pack(side='top', fill='x', padx='5', pady='10') would be". With the "side"attribute, you can determine the Position in the GUI. With the "fill"attribute, you can customize the height and width of a GUI component at the specified frame. With "padx" and "pady", you can set the room to a GUI component around.
  • Note, however, that you can use in a Python program, either the grid or pack geometry Manager. But never both at the same time.

GUI programming: Buttons add

Very often in programs Buttons find. In Python, there are three important Standard Buttons:
  • The normal Button, you can add, for example, with the command "button1 = Button(text="Test", bg="red", fg="black", command=test1)" (without the quotation marks). When this Button is pressed, then the function "test1 () is executed".
  • In addition to the normal Buttons, there are also so-called check button. An example of this is checkbutton1 = Checkbutton(root, text="Test", onvalue=1, offvalue=0, variable=var1) would be "". It is important that you insert the command "var1 = IntVar ()". The only way the program knows that the Variable "var1" is an Integer Variable. If the Checkbox is checked, "var1" to the value "1" (onvalue). If the Checkbox is not activated, it is assumed the value 0 (offvalue). Tip: you can use the command "var1.get()" to find out.
  • Finally, there is the radio button. These you can add, for example, with the command "radiobutton1 = Radiobutton(root, text="Test", value=1)". All radio Buttons that have "value" to the same value will be enabled as soon as one of these Buttons is activated. Therefore, you should enter in the "value" each Time a different value, so that only one Button will be activated.
  • Do not forget by the way to manage your Buttons with the grid or pack geometry Manager.
GUI programming: Buttons

Python: Terminal hide

Normally appears each time the Python program, the Terminal, whether with or without a GUI. Especially in the case of programs with a GUI however, this is very annoying. However, you can hide this also. How to do that in Windows 10, we will show you now:
  1. Start Explorer and click on the "view"tab.
  2. Now, ensure that the Checkbox "file name extensions" is checked.
  3. Rename your Python file so that the file extension is not more .py but .pyw is.
Python: Terminal hide

More tips to Python in our CHIP-counselors

Tip: On our topic page for the Python programming language, you can find more Tutorials about programming. There we will show you, for example, as the range and xrange functions use can.

YOU MAY ALSO LIKE

0 COMMENTS

LEAVE A COMMENT

Human?
1 + 3 =