In this example, I’ll create a hello world application using Tkinter in Python 3.
Output:
Python 3 Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | from tkinter import * window = Tk() window.title("www.code4example.net") window.geometry("400x200") app = Frame(window) app.grid() w = Label(app, text="Hello, world!",font=("Courier", 20)) w.grid(padx=110, pady=80) window.mainloop() |
Download hello world python tkinter
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
Tk
object calledwindow
, which represents the main window of the GUI. - The
title
method sets the title of the window to “www.code4example.net“, and thegeometry
method sets the size of the window to 400 pixels wide and 200 pixels tall. - A
Frame
object calledapp
is created, which will contain the other widgets (elements) of the GUI. Thegrid
method is called to set up a grid layout for the widgets. - A
Label
widget calledw
is created, which displays the text “Hello, world!” in a large, Courier font. Thegrid
method is called to position the label within theapp
frame. - The
mainloop
method 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.