In this example, I’ll create a hello world application using Tkinter in Python 3.
Python 3 Code:
from tkinter import *
window = Tk()
app = Frame(window)
w = Label(app, text="Hello, world!",font=("Courier", 20))
This code creates a simple graphical user interface (GUI) using the Tkinter library in Python. Here is a breakdown of what the code does:
- The first two lines import the Tkinter library and create a
window, which represents the main window of the GUI.
titlemethod sets the title of the window to “www.code4example.net“, and the
geometrymethod sets the size of the window to 400 pixels wide and 200 pixels tall.
appis created, which will contain the other widgets (elements) of the GUI. The
gridmethod is called to set up a grid layout for the widgets.
wis created, which displays the text “Hello, world!” in a large, Courier font. The
gridmethod is called to position the label within the
mainloopmethod is called to start the GUI event loop, which listens for user input and updates the GUI as necessary.
When the code is run, it will create a window with a label that says “Hello, world!” in a large, Courier font. The window will remain open until the user closes it.