- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Tkinter widgets
In this part of the Tkinter tutorial, we will cover some basic Tkinter widgets. We work with the following widgets: Checkbutton
, Label
, Scale
, and Listbox
.
Widgets are basic building blocks of a GUI application. Over the years, several widgets became a standard in all toolkits on all OS platforms; for example a button, a check box or a scroll bar. Some of them might have different names. For instance, a check box is called a check button in Tkinter. Tkinter has a small set of widgets which cover basic programming needs. More specialised widgets can be created as custom widgets.
Checkbutton
Checkbutton
is a widget that has two states: on and off. The on state is visualized by a check mark. (Some themes may have different visuals.) It is used to denote some boolean property. The Checkbutton
widget provides a check box with a text label.
checkbutton.py
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode Tkinter tutorial This program toggles the title of the window with the Checkbutton widget. Author: Jan Bodnar Last modified: November 2015 Website: www.zetcode.com """ from Tkinter import Tk, Frame, Checkbutton from Tkinter import BooleanVar, BOTH class Example(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.initUI() def initUI(self): self.parent.title("Checkbutton") self.pack(fill=BOTH, expand=True) self.var = BooleanVar() cb = Checkbutton(self, text="Show title", variable=self.var, command=self.onClick) cb.select() cb.place(x=50, y=50) def onClick(self): if self.var.get() == True: self.master.title("Checkbutton") else: self.master.title("") def main(): root = Tk() root.geometry("250x150+300+300") app = Example(root) root.mainloop() if __name__ == '__main__': main()
In our example, we place a check button on the window. The check button shows or hides the title of the window.
self.var = BooleanVar()
We create an BooleanVar
object. It is a value holder for Boolean values for widgets in Tkinter.
cb = Checkbutton(self, text="Show title", variable=self.var, command=self.onClick)
An instance of the Checkbutton
is created. The value holder is connected to the widget via the variable
parameter. When we click on the check button, the onClick()
method is called. This is done with the command
parameter.
cb.select()
Initially, the title is shown in the titlebar. So at the start, we make it checked with the select()
method.
if self.var.get() == True: self.master.title("Checkbutton") else: self.master.title("")
Inside the onClick()
method, we display or hide the title based on the value from the self.var
variable.

Label
The Label
widget is used to display text or images. No user interaction is available.
sudo apt-get install python-imaging-tk
In order to run this example, we must install python-imaging-tk
module.
label.py
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode Tkinter tutorial In this script, we use the Label widget to show an image. Author: Jan Bodnar Last modified: November 2015 Website: www.zetcode.com """ from PIL import Image, ImageTk from Tkinter import Tk, Frame, Label class Example(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.initUI() def initUI(self): self.parent.title("Label") self.img = Image.open("tatras.jpg") tatras = ImageTk.PhotoImage(self.img) label = Label(self, image=tatras) # reference must be stored label.image = tatras label.pack() self.pack() def setGeometry(self): w, h = self.img.size self.parent.geometry(("%dx%d+300+300") % (w, h)) def main(): root = Tk() ex = Example(root) ex.setGeometry() root.mainloop() if __name__ == '__main__': main()
Our example shows an image on the window.
from PIL import Image, ImageTk
By default, the Label
widget can display only a limited set of image types. To display a JPG image, we must use the PIL, Python Imaging Library module.
self.img = Image.open("tatras.jpg") tatras = ImageTk.PhotoImage(self.img)
We create an image from the image file in the current working directory. Later we create a photo image from the image.
label = Label(self, image=tatras)
The photoimage is given to the image
parameter of the label widget.
label.image = tatras
In order not to be garbage collected, the image reference must be stored.
w, h = self.img.size self.parent.geometry(("%dx%d+300+300") % (w, h))
We make the size of the window to exactly fit the image size.
Scale
Scale
is a widget that lets the user graphically select a value by sliding a knob within a bounded interval. Our example will show a selected number in a label widget.
scale.py
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode Tkinter tutorial In this script, we show how to use the Scale widget. Author: Jan Bodnar Last modified: November 2015 Website: www.zetcode.com """ from ttk import Frame, Label, Scale, Style from Tkinter import Tk, BOTH, IntVar, LEFT class Example(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.initUI() def initUI(self): self.parent.title("Scale") self.style = Style() self.style.theme_use("default") self.pack(fill=BOTH, expand=1) scale = Scale(self, from_=0, to=100, command=self.onScale) scale.pack(side=LEFT, padx=15) self.var = IntVar() self.label = Label(self, text=0, textvariable=self.var) self.label.pack(side=LEFT) def onScale(self, val): v = int(float(val)) self.var.set(v) def main(): root = Tk() ex = Example(root) root.geometry("250x100+300+300") root.mainloop() if __name__ == '__main__': main()
We have two widgets in the above script: a scale and a label. A value from the scale widget is shown in the label widget.
scale = Scale(self, from_=0, to=100, command=self.onScale)
A Scale
widget is created. We provide the lower and upper bounds. The from is a regular Python keyword that is why there is an underscore after the first parameter. When we move the knob of the scale, the onScale()
method is called.
self.var = IntVar() self.label = Label(self, text=0, textvariable=self.var)
An integer value holder and label widget are created. Value from the holder is shown in the label widget.
def onScale(self, val): v = int(float(val)) self.var.set(v)
The onScale()
method receives a currently selected value from the scale widget as a parameter. The value is first converted to a float and then to integer. Finally, the value is set to the value holder of the label widget.

Listbox
Listbox
is a widget that displays a list of objects. It allows the user to select one or more items.
listbox.py
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode Tkinter tutorial In this script, we show how to use the Listbox widget. Author: Jan Bodnar Last modified: November 2015 Website: www.zetcode.com """ from ttk import Frame, Label from Tkinter import Tk, BOTH, Listbox, StringVar, END class Example(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.parent = parent self.initUI() def initUI(self): self.parent.title("Listbox") self.pack(fill=BOTH, expand=1) acts = ['Scarlett Johansson', 'Rachel Weiss', 'Natalie Portman', 'Jessica Alba'] lb = Listbox(self) for i in acts: lb.insert(END, i) lb.bind("<<ListboxSelect>>", self.onSelect) lb.pack(pady=15) self.var = StringVar() self.label = Label(self, text=0, textvariable=self.var) self.label.pack() def onSelect(self, val): sender = val.widget idx = sender.curselection() value = sender.get(idx) self.var.set(value) def main(): root = Tk() ex = Example(root) root.geometry("300x250+300+300") root.mainloop() if __name__ == '__main__': main()
In our example, we show a list of actresses in the Listbox
. The currently selected actress is displayed in a label widget.
acts = ['Scarlett Johansson', 'Rachel Weiss', 'Natalie Portman', 'Jessica Alba']
This is a list of actresses to be shown in the listbox.
lb = Listbox(self) for i in acts: lb.insert(END, i)
We create an instance of the Listbox
and insert all the items from the above mentioned list.
lb.bind("<<ListboxSelect>>", self.onSelect)
When we select an item in the listbox, the <<ListboxSelect>>
event is generated. We bind the onSelect()
method to this event.
self.var = StringVar() self.label = Label(self, text=0, textvariable=self.var)
A label and its value holder is created. In this label we will display the currently selected item.
sender = val.widget
We get the sender of the event. It is our listbox widget.
idx = sender.curselection()
We find out the index of the selected item using the curselection()
method.
value = sender.get(idx)
The actual value is retrieved with the get()
method, which takes the index of the item.
self.var.set(value)
Finally, the label is updated.

In this part of the Tkinter tutorial, we have presented several Tkinter widgets.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论