0% found this document useful (0 votes)
3 views18 pages

Tkinter8_09abd08371cd27ea4300250e6135585c

Tkinter is a Python interface to the Tk GUI library, part of the Python standard library since 1994, primarily used for data-driven applications and simple utilities. The document provides instructions for installing Python 3 and Tkinter on Windows, as well as code snippets for creating basic Tkinter windows and common widgets like buttons, labels, and entry fields. It also explains the differences in importing styles and the structure of Tkinter applications.

Uploaded by

roycee2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views18 pages

Tkinter8_09abd08371cd27ea4300250e6135585c

Tkinter is a Python interface to the Tk GUI library, part of the Python standard library since 1994, primarily used for data-driven applications and simple utilities. The document provides instructions for installing Python 3 and Tkinter on Windows, as well as code snippets for creating basic Tkinter windows and common widgets like buttons, labels, and entry fields. It also explains the differences in importing styles and the structure of Tkinter applications.

Uploaded by

roycee2023
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Python Tkinter

Python Tkinter
Tkinter (Tk)
• Tkinter widget library originates from the Tool Command Language (Tcl)
programming language.
• Tcl and Tk were created by John Ousterman while he was a professor at Berkeley in
the late 1980s as an easier way to program engineering tools being used at the
university.
• Tkinter is a Python interface to the Tk GUI library and has been a part of the Python
standard library since 1994 with the release of Python version 1.1, making it the de
facto GUI library for Python
Python Tkinter
• Tkinter might be the wrong choice for a game UI or slick commercial application;
• however, for data-driven applications, simple utilities, configuration dialogs, and other
business logic applications, Tkinter offers all that is needed and more.
Installing Python 3 /Tkinter on Windows
• You can obtain Python 3 installers for Windows from the python.org website by
performing the following steps:
1. Go to http://www.python.org/downloads/windows.
2. Select the latest Python 3 release.
3. Under the Files section, select the Windows executable installer appropriate to your
system's architecture (x86 for 32-bit Windows OR x86_64 for 64-bit Windows).
4. Launch the downloaded installer.
5. Click on Customize installation. Make sure the tcl/tk and IDLE option is checked
(it should be by default).
6. Continue through the installer with all defaults.
• Note: To find out if Tkinter is installed, open a CMD Terminal and try the following
command: python3 -m tkinter
Basic Tkinter Window
To create Basic Tkinter Window To Add Title
root = Tk()
from tkinter import * root.title(‘My Tkinter Project’)
from tkinter.ttk import * label = Label(root, text="Hello World")
root = Tk() label.pack()
root.mainloop() root.mainloop()

Set window width and height in pixels


To Add Labels root = Tk()
root = Tk() root.title(‘My Tkinter Project’)
root.geometry(‘800x500’)
label = Label(root, text="Hello World") label = Label(root, text="Hello World")
label.pack() label.pack()
root.mainloop() root.mainloop()
Basic Tkinter Window
import tkinter as tk
root = tk.Tk() # Create main window
label = tk.Label(root, text="Hello, Tkinter!") # Add a label
label.pack()
root.mainloop() # Start the event loop
Meaning Breakdown
• tk is the alias for the tkinter module (assuming you did import tkinter as tk).
• Tk() is a class in tkinter that initializes the main application window.
• root is a variable holding a reference to that main window.
• root = tk.Tk(): It acts as the parent container for all widgets in the GUI.
Basic Tkinter Window
Difference between importing style Difference between importing style
import tkinter as tk from tkinter import *
from tkinter import ttk • This imports everything from tkinter
• It imports the whole tkinter module and gives it directly into your namespace.
the alias tk.
• You can use Label, Button, etc.,
• You must prefix all widgets with tk., like tk.Label, without any prefix.
tk.Button, etc.
• This is the recommended and cleanest way to
import Tkinter.
Basic Tkinter Window
Difference between importing style
from tkinter.ttk import
• This imports widgets from the ttk module, which is part of tkinter.
• ttk stands for Themed Tkinter widgets.
• These are modern-looking widgets with more styling options. Examples: ttk.Button,
ttk.Label, ttk.Combobox, etc.
Basic Tkinter Window
Difference between importing style
Basic Tkinter Window
label = Label(root, text="Hello World"):
• This creates a new Label object. A Label object is just a widget for displaying text (or
images).
Feature in the label object:
label = Label(root, text="Hello World")
• The first argument we pass to Label() is the parent or master widget. Tkinter widgets
are arranged in a hierarchy starting with the root window.
• The second argument is a keyword argument that specifies the text to be displayed
on the Label object.
• We store the new Label instance in a variable, label.
• label.pack(): This places the new label widget onto its parent widget. In this case,
we're using the pack() method, which is the simplest of three geometry manager
methods you can use
Basic Tkinter Window
root.mainloop():
• This final line starts our main event loop. This loop is responsible for processing all
the events—keystrokes, mouse clicks etc.
• it will run until the program is quit or end.
• This is usually the last line of any Tkinter script, since any code after it won't run until
the main window is closed.
Common Python Tkinter widgets and code snippets
Button( A clickable)
• button = tk.Button(root, text="Click Me", command=lambda: print("Button clicked!"))
button.pack()

Entry(Single-line text input)


• entry = tk.Entry(root)
entry.pack()

Text(Multi-line text input)


• text = tk.Text(root, height=5, width=30)
text.pack()
Common Python Tkinter widgets and code snippets
Checkbutton (A checkbox for toggling options)
• var = tk.IntVar()
check = tk.Checkbutton(root, text="Check me", variable=var)
check.pack()

Radiobutton (Used for selecting one option from a group.)


• var = tk.StringVar()
radio1 = tk.Radiobutton(root, text="Option 1", variable=var, value="1")
radio2 = tk.Radiobutton(root, text="Option 2", variable=var, value="2")
radio1.pack()
radio2.pack()
Common Python Tkinter widgets and code snippets
Scale(Slider for selecting a value)
• scale = tk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL)
scale.pack()

Listbox(Displays a list of selectable items.)


• listbox = tk.Listbox(root)
listbox.insert(1, "Item 1")
listbox.insert(2, "Item 2")
listbox.pack()
Common Python Tkinter widgets and code snippets
Canvas (Used for drawing shapes and images)
• canvas = tk.Canvas(root, width=200, height=100)
canvas.create_rectangle(50, 25, 150, 75, fill="blue")
canvas.pack()

Frame(Container widget to group other widgets)


• frame = tk.Frame(root, borderwidth=2, relief="sunken")
frame.pack()
tk.Label(frame, text="Inside Frame").pack()
Common Python Tkinter widgets and code snippets
Message (Like a label but for multi-line messages)
• message = tk.Message(root, text="This is a message widget")
message.pack()

Menu(Creates drop-down menus)


• def hello(): print("Hello!")
menubar = tk.Menu(root)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Say Hello", command=hello)
menubar.add_cascade(label="File", menu=filemenu)
root.config(menu=menubar)
Common Python Tkinter widgets and code snippets
Spinbox(Like Entry, but for numeric ranges)
• spinbox = tk.Spinbox(root, from_=0, to=10)
spinbox.pack()

Paned(WindowContainer to manage child widgets in panes)


paned = tk.PanedWindow(root)
left = tk.Label(paned, text="Left Pane")
right = tk.Label(paned, text="Right Pane")
paned.add(left)
paned.add(right)
paned.pack()
Common Python Tkinter widgets and code snippets
Scrollbar(Adds scrollbars to widgets like Text, Canvas, or Listbox)
• scrollbar = tk.Scrollbar(root)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
listbox = tk.Listbox(root, yscrollcommand=scrollbar.set)
for i in range(100):
listbox.insert(tk.END, f"Item {i}")
listbox.pack(side=tk.LEFT, fill=tk.BOTH)
scrollbar.config(command=listbox.yview)

You might also like