Tkinter Tutorial Python Pdf Download
A Python Book Contents 1 Part 1 Beginning Python.10. Python GUI examples (Tkinter Tutorial) 2018--08-27 Comments(45) In this tutorial, we will learn how to develop graphical user interfaces by writing some Python GUI examples using Tkinter package. A Comprehensive Introduction to Python Programming and GUI Design Using Tkinter Bruno Dufour McGill Univeristy SOCS. 2 Python and Tkinter for RAD and Prototyping 16. Python is an interpreted, interactive, object-oriented high-level language. A tutorial on how to use Python, and the Tkinter library, to create some common Graphical User Interface (GUI) patterns, like progress bars and text windows. Or the book Python and Tkinter Programming by John Grayson (Manning, 2000, ISBN 1-884777-81-3). New Mexico Tech Computer Center Tkinter reference: A GUI for Python Page 2. Part I: The face of your application We’ll start by looking at the visible part of Tkinter: creating the widgets and arranging.
Tkinter is a Python wrapper for Tcl/Tk providing a cross-platform GUI toolkit. On Windows, it comes bundled with Python; on other operating systems, it can be installed. The set of available widgets is smaller than in some other toolkits, but since Tkinter widgets are extensible, many of the missing compound widgets can be created using the extensibility, such as combo box and scrolling pane.
IDLE, Python's Integrated Development and Learning Environment, is written using Tkinter and is often distributed with Python. You can learn about features of Tkinter by playing around with menus and dialogs of IDLE. For instance, Options > Configure IDLE.. dialog shows a broad variety of GUI elements including tabbed interface. You can learn about programming using Tkinter by studying IDLE source code, which, on Windows, is available e.g. in C:Program FilesPython27Libidlelib.
Python 3: The examples on this page are for Python 2. In Python 3, what was previously module Tkinter is tkinter, what was tkMessageBox is messagebox, etc.
A minimal example:
A minimal example made more compact - later references to GUI items not required:
A minimal example creating an application class derived from Frame:
Simple message boxes can be created using tkMessageBox as follows:
- The tkMessageBox dialogs module, infohost.nmt.edu
- Standard Dialogs, effbot.org
File dialogs can be created as follows:
- The tkFileDialog module, infohost.nmt.edu
- File Dialogs, effbot.org
- tkFileDialog, tkinter.unpythonic.net
A radio button can be used to create a simple choice dialog with multiple options:
An alternative to radio button that immediately reacts to button press:
- The Radiobutton widget, infohost.nmt.edu
- The Tkinter Radiobutton Widget, effbot.org
A list box can be used to create a simple multiple-choice dialog:
- The Listbox widget, infohost.nmt.edu
- The Tkinter Listbox Widget, effbot.org
- TKinter Listbox example (Python recipe), code.activestate.com
Checkbox or check button can be created as follows:
- The Checkbutton widget, infohost.nmt.edu
- The Tkinter Checkbutton Widget, effbot.org
Entry widget, a single-line text input field, can be used as follows:
- The Entry widget, infohost.nmt.edu
- The Tkinter Entry Widget, effbot.org
Menus can be created as follows:
- The Menu widget, infohost.nmt.edu
- The Tkinter Menu Widget, effbot.org
A frame around other elements can be created using LabelFrame widget as follows:
- The LabelFrame widget, infohost.nmt.edu
- Tkinter LabelFrame Widget, effbot.org
Message is like Label but ready to wrap across multiple lines. An example:
- The Message widget, infohost.nmt.edu
- The Tkinter Message Widget, effbot.org
Drop-down list, in Tkinter option menu, can be created as follows:
- The OptionMenu widget, infohost.nmt.edu
- The Tkinter OptionMenu Widget, effbot.org
Text widget is a more complex one, allowing editing of both plain and formatted text, including multiple fonts.
Example to be added.
- The Text widget, infohost.nmt.edu
- The Tkinter Text Widget, effbot.org
The Windows installer for Python 2.3 ships with Tcl/Tk 8.4.3. You can find out about the version:
- What's new in Python 2.3, python.org
- 24.1. Tkinter — Python interface to Tcl/Tk, python.org
- Tkinter 8.5 reference: a GUI for Python, infohost.nmt.edu; the same as pdf
- An Introduction to Tkinter, effbot.org
- Tkinter Summary, staff.washington.edu
- TkInter, wiki.python.org
- GUI Tk « Python, java2s.com, many examples
In this tutorial, we will learn how to develop graphical user interfaces by writing some Python GUI examples using the Tkinter package.
Join the DZone community and get the full member experience.Join For Free
In this tutorial, we will learn how to develop graphical user interfaces by writing some Python GUI examples using the Tkinter package.
Tkinter package is shipped with Python as a standard package, so we don't need to install anything to use it.
Tkinter is a very powerful package. If you already have installed Python, you may use IDLE which is the integrated IDE that is shipped with Python, this IDE is written using Tkinter. Sounds Cool!!
We will use Python 3.6, so if you are using Python 2.x, it's strongly recommended to switch to Python 3.x unless you know the language changes so you can adjust the code to run without errors.
I assume that you have a little background in the Python basics to help you understand what we are doing.
We will start by creating a window to which we will learn how to add widgets such as buttons, combo boxes, etc. Then we will play with their properties, so let's get started.
Create Your First GUI Application
First, we will import THE Tkinter package and create a window and set its title:
The result will look like this:
Awesome! Our application works.
The last line calls the
mainloopfunction. This function calls the endless loop of the window, so the window will wait for any user interaction till we close it.
If you forget to call the
mainloopfunction, nothing will appear to the user.
Create a Label Widget
To add a label to our previous example, we will create a label using the label class like this:
Then we will set its position on the form using the
gridfunction and give it the location like this:
So the complete code will be like this:
And this is the result:
Without calling the
gridfunction for the label, it won't show up.
Set Label Font Size
You can set the label font so you can make it bigger and maybe bold. You can also change the font style.
To do so, you can pass the
fontparameter like this:
Note that the
fontparameter can be passed to any widget to change its font, thus it applies to more than just labels.
Great, but the window is so small, what about setting the window size?
Setting Window Size
We can set the default window size using the
geometryfunction like this:
The above line sets the window width to 350 pixels and the height to 200 pixels.
Let's try adding more GUI widgets like buttons and see how to handle button click events.
Adding a Button Widget
Let's start by adding the button to the window. The button is created and added to the window in the same way as the label:
So our window will be like this:
The result looks like this:
Note that we place the button on the second column of the window, which is 1. If you forget and place the button on the same column which is 0, it will show the button only, since the button will be on the top of the label.
Change Button Foreground and Background Colors
You can change the foreground of a button or any other widget using the fg property.
Also, you can change the background color of any widget using the bg property.
Now, if you tried to click on the button, nothing happens because the click event of the button isn't written yet.
Handle Button Click Event
First, we will write the function that we need to execute when the button is clicked:
May 07, 2016 Windows 7 Home Premium 32/64 bit ISO free download full free trial version latest version offline installer standalone single highly compressed google drive, torrent direct download setup. It is a ISO bootabel image of Windows 7 Home Premium. Download windows 7. Jun 17, 2015 Windows 7 Home Basic Full Version Free Download ISO 32 / 64 Bit by Softlay Editor Updated 19 April, 2018 Microsoft Windows 7 is available in six different editions ( Starter, Home basic, Home premium, Enterprise, Professional and Ultimate). Click on below button to start GW Basic Free Download. This is complete offline installer and standalone setup for GW Basic. This is complete offline installer and standalone setup for GW Basic. This would be compatible with both 32 bit and 64 bit windows. Windows 7 Home Basic Free Download 32 bit 64 Bit ISO In single link. Here is bootable ISO image Windows 7 Dvd Home Basic for PC. Windows 7 Home Basic Product Review: Simplicity is all what Windows 7 Home Basic is about. Download visual basic for windows 7 32 bit for free. Development Tools downloads - Microsoft Visual Basic by Microsoft and many more programs are available for instant and free download.
Then we will wire it with the button by specifying the function like this:
btn = Button(window, text= 'Click Me', command=clicked)
Note that, we typed
clicked only not
clicked() with parentheses.
Now the full code will be like this:
And when we click the button, the result, as expected, looks like this:
Get Input Using Entry Class (Tkinter Textbox)
In the previous Python GUI examples, we saw how to add simple widgets, now let's try getting the user input using the Tkinter
Entryclass (Tkinter textbox).
You can create a textbox using Tkinter
Entryclass like this:
Then you can add it to the window using a grid function as usual
So our window will be like this:
And the result will be like this:
Now, if you click the button, it will show the same old message, but what about showing the entered text on the Entry widget?
First, you can get entry text using the
getfunction. So we can write this code to our clicked function like this:
If you click the button and there is text in the entry widget, it will show 'Welcome to' concatenated with the entered text.
And this is the complete code:
Run the above code and check the result:
Every time we run the code, we need to click on the entry widget to set focus to write the text, but what about setting the focus automatically?
Set the Focus of the Entry Widget
That's super easy, all we need to do is to call the
focus function like this:
And when you run your code, you will notice that the entry widget has the focus so you can write your text right away.
Disable the Entry Widget
To disable the entry widget, you can set the state property to disabled:
Now, you won't be able to enter any text.
Add a Combobox Widget
To add a combobox widget, you can use the
Comboboxclass from ttk library like this:
Then you can add your values to the combobox.
As you can see, we add the combobox items using the values tuple.
To set the selected item, you can pass the index of the desired item to the current function.
To get the select item, you can use the
getfunction like this:
Add a Checkbutton Widget (Tkinter Checkbox)
To create a checkbutton widget, you can use the
Checkbuttonclass like this:
Also, you can set the checked state by passing the check value to the Checkbutton like this:
Check the result:
Set the Check State of a Checkbutton
Here we create a variable of type
BooleanVarwhich is not a standard Python variable, it's a Tkinter variable, and then we pass it to the Checkbutton class to set the check state as the highlighted line in the above example.
You can set the Boolean value to false to make it unchecked.
Also, you can use
IntVar instead of
BooleanVarand set the value to 0 or 1.
These examples give the same result as the
Tkinter Tutorial Python 3 Pdf
Add Radio Button Widgets
To add radio buttons, you can use the
RadioButtonclass like this:
Note that you should set the value for every radio button with a different value, otherwise, they won't work.
The result of the above code looks like this:
Also, you can set the command of any of these radio buttons to a specific function, so if the user clicks on any one of them, it runs the function code.
This is an example:
Get Radio Button Values (Selected Radio Button)
To get the currently selected radio button or the radio button's value, you can pass the variable parameter to the radio buttons and later you can get its value.
Every time you select a radio button, the value of the variable will be changed to the value of the selected radio button.
Add a ScrolledText Widget (Tkinter textarea)
To add a ScrolledText widget, you can use the
ScrolledText class like this:
Here we specify the width and the height of the ScrolledText widget, otherwise, it will fill the entire window.
The result as you can see:
Set Scrolledtext Content
To set scrolledtext content, you can use the
insertmethod like this:
Delete/Clear Scrolledtext Content
To clear the contents of a scrolledtext widget, you can use the
deletemethod like this:
Create a Message Box
To show a message box using Tkinter, you can use the messagebox library like this:
Let's show a message box when the user clicks a button.
When you click the button, an informative message box will appear.
Show Warning and Error Messages
You can show a warning message or error message the same way. The only thing that needs to be changed is the message function
Show Ask Question Dialogs
To show a yes/no message box to the user, you can use one of the following
You can choose the appropriate message style according to your needs. Just replace the
showinfofunction line from the previous line and run it.
Also, you can check what button was clicked using the result variable.
If you click OK or yes or retry, it will return True as the value, but if you choose no or cancel, it will return False.
The only function that returns one of three values is the
askyesnocancel function; it returns True or False or None.
Add a SpinBox (Numbers Widget)
To create a Spinbox widget, you can use the
Spinboxclass like this:
Here we create a Spinbox widget and we pass the from_ and to parameters to specify the numbers range for the Spinbox.
Spinrite free download cnet downloads. Also, you can specify the width of the widget using the width parameter:
Check the complete example:
You can specify the numbers for the Spinbox instead of using the whole range like this:
Here the Spinbox widget only shows these 3 numbers: 3, 8, and 11.
Set a Default Value for Spinbox
To set the Spinbox default value, you can pass the value to the
textvariableparameter like this:
Now, if you run the program, it will show 36 as a default value for the Spinbox.
Add a Progressbar Widget
To create a progress bar, you can use the
progressbarclass like this:
You can set the progress bar value like this:
You can set this value based on any process you want like downloading a file or completing a task.
Change Progressbar Color
Changing the Progressbar color is a bit tricky.
First, we will create a style and set the background color and finally set the created style to the Progressbar.
Check the following example:
And the result will look like this:
Add a File Dialog (File and Directory Chooser)
To create a file dialog (file chooser), you can use the
filedialogclass like this:
After you choose a file and click open, the file variable will hold that file path.
Also, you can ask for multiple files like this:
Specify File Types (Filter File Extensions)
You can specify the file types for a file dialog using the
filetypes parameter, just specify the extensions in tuples.
You can ask for a directory using the
You can specify the initial directory for the file dialog by specifying the
Add a Menu Bar
To add a menu bar, you can use the
menuclass like this:
First, we create a menu, then we add our first label, and, finally, we assign the menu to our window.
You can add menu items under any menu by using the
add_cascade()function like this:
So our code will be like this:
This way, you can add as many menu items as you want.
Here we add another menu item called Edit with a menu separator.
You may notice a dashed line at the beginning, well, if you click that line, it will show the menu items in a small separate window.
You can disable this feature by disabling the
tearofffeature like this:
Just replace the
new_itemin the above example with this one and it won't show the dashed line anymore.
I don't need to remind you that you can type any code that works when the user clicks on any menu item by specifying the command property.
Add a Notebook Widget (Tab Control)
To create a tab control, there are a few steps.
- First, we create a tab control using the
- Create a tab using the
- Add that tab to the tab control.
- Pack the tab control so it becomes visible in the window.
In this way, you can add as many tabs as you want.
Add Widgets to Notebooks
After creating tabs, you can put widgets inside these tabs by assigning the parent property to the desired tab.
Add Spacing for Widgets (Padding)
You can add padding for your controls to make it look well organized using the
padyto any widget and give them a value.
It's that simple!
In this tutorial, we saw many Python GUI examples using the Tkinter library and we saw how easy it's to develop graphical interfaces using it.
This tutorial covered the main aspects of Python GUI development, but not all of them. There is no tutorial or a book that can cover everything.
I hope you found these examples useful. Keep coming back.
Like This Article? Read More From DZone
Published at DZone with permission of Mokhtar Ebrahim , DZone MVB. See the original article here.
Tkinter Tutorial Python Pdf Downloads
Opinions expressed by DZone contributors are their own.