Up to this point, we've seen how to handle the properties and methods of Tkinter's widgets. Now, we will learn the basic steps to write real-world applications.
Tkinter is really powerful, and if you are not satisfied with the widgets that it offers, you can create your own set of widgets. A very interesting and customized widget that you should consider checking before learning how to create your own, is the TreeWidget, which is part of the latest idle distribution.
This widget uses a Tk Canvas widget and some images to nicely simulate the TreeView Windows control. The simplest windowing application that you can create consists of just one window, which is called the root window. The root window is created using the Tk() call.from Tkinter import *
If your application needs more than just one single window, you can use the Toplevel widget to create additional windows for you. This widget has a behavior very similar to the window generated byTk(). This widget also dispenses the use of geometry management functions because the window manager displays this widget, immediately after you call it.from Tkinter import *
After adding a lot of windows to your application, maybe now you are wondering whether it would be OK to add a menu to your program. The following code does that for you.from Tkinter import *
What's next? What about adding a toolbar to our little application? The simplest way to implement a toolbar is by taking a Frame widget and storing all the required buttons on it.from Tkinter import *
As we want our toolbar to be on the highest area of our screen, we have to pack it on the top side of the Frame widget. The fill option being set to X in the toolbar widget enables the toolbar to extend itself, covering the entire extension of the parent frame size.
Note the usage of the PhotoImage class. This class is used to load the GIF files from disk and store them into variables. Then, these variables are passed to the Button options that handle images. Let's move forward now. The next step is to create a status bar for our small application. We want this bar to be on the bottom side of the window.from Tkinter import *
As you could see, we used the Label widget to implement the statusbar in order to be able to change the text value later. Pretty nice, isn't it? Now, have a look at the final shape of our interface.
This figure shows the complete example of designing the structure of an applicationsing Tkinter.
In my opinion, one of the greatest things about designing GUI applications using Tkinter is the number
of things that are already done and ready to be used by your applications. Some examples are the
following modules, which are part of the Tkinter distribution, and implement common dialog boxes.
The usage of these modules is very simple. The next example opens a file dialog box, which allows you to browse the files through your local directory, and returns the filename selected .
Note that the FileDialog returns the name of the selected file, and our application showsthat name on the status bar.
from Tkinter import *
As you could notice, this is a very simple example of the power of Tkinter, but the concept of creating a dialog is as simple as the concept of creating a window.
After creating a Toplevel widget and making the call to open the dialog, the standard dialog only returns to the Toplevel widget when it is closed. When you start facing problems like this, you have several solution options, such as opening several dialogs and making them run in parallel. Or you can create the dialog and only return the control back to the Toplevel widget when the dialogs are closed by the user, creating a modal behavior. This solution is implemented using the wait_window method, which creates a local event loop, and only returns when the window informed as an argument is closed.
Although application modal dialogs are easier to program, most users find them much more annoying. If possible, only use modal dialogs where some action has to be performed before the application can continue.
That's it. Our overview about Tkinter ends here. Next you will see a toolkit that extends the set of available widgets you can use, and next you will find a list of useful resources for a more advanced approach on this topic.
Python Related Interview Questions
|Perl Scripting Interview Questions||C++ Interview Questions|
|PHP Interview Questions||C Interview Questions|
|Ruby on Rails Interview Questions||Ruby Interview Questions|
|Django Interview Questions||Lisp programming Interview Questions|
|R Programming language Interview Questions||wxPython Interview Questions|
|Python Automation Testing Interview Questions|
Extending And Embedding Python
Objects Interfacing And Distribution
Working With Databases
Other Advanced Topics
Basic Network Background
Python And Guis
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.