Python Notes1
Python Notes1
Tkinter Programming
Tkinter is the standard GUI library available for python. Python when combined with Tkinter, provides a fast and easy way
to create a GUI applications.
Here are the steps listed, to keep in mind for creating any GUI applications in python:
import tkinter;
top = tkinter.Tk();
# the code will go here to add widgets...
top.mainloop();
It will create a window with title tk as shown in the sample screenshot given below.
import tkinter;
print(tkinter.TkVersion);
Root Window
Basically, the foundation of a GUI python program is its root window, upon which you can add all the other GUI elements.
In simple, if GUI is a tree, then root window is its root. It means, the tree (GUI) can branch out in all directions, but every of
its part are anchored by the root (root window) either directly or indirectly.
Above python code fragment import all the functions of tkinter module.
To create a root window in python GUI (Graphical User Interface) programming or development, follow the code given
below:
root = Tk();
Here is an example shows a simplest and first GUI windows created using python.
If you will run or execute the above GUI code of python, then here is the output you will see on your screen:
Modifing a Root Window
Now let's modify a root window as created earlier with some of the given code fragment of Python GUI programming.
import tkinter;
mainWin = tkinter.Tk();
mainWin.title("CodesCracker Online Python Coding");
mainWin.geometry("400x400");
GUI (Graphical User Interface) elements are widgets or windows gadgets. The simplest windows gadget is the Label
windows gadget which is non-editable icons or text or both.
Create a Frame
In Python GUI, a frame is simply a widget (windows gadget) that can hold other widgets.
myApp = Frame(root);
Or
app = Frame(mainWin);
Create a Label
Let's create a label in Python GUI using the following code.
lab.grid();
lab1.grid();
mainWin.mainloop();
In Python GUI, buttons are the widgets that can be activated by the user just to perform some action.
Python GUI Create Buttons
win.mainloop();
Here is the sample output produced by the above creating buttons using python example code.
Let's take another example of creating buttons in python gui with another way as shown below.
As you already knows that organizing your python code into classes makes your programming task more easier.
def create_widgets(self):
self.myFirstButton = Button(self, text = "This is first button");
self.myFirstButton.grid();
self.mySecondButton = Button(self, text = "This is second button");
self.mySecondButton.grid();
self.myThirdButton = Button(self, text = "This is third button");
self.myThirdButton.grid();
myWin = Tk();
myWin.title("Python GUI using class");
myWin.geometry("300x100");
app = MyApp(myWin);
myWin.mainloop();
Here is the sample output produced by the above example code of creating python gui using class.
In python, the entry widget is good only for one line of text, whereas the text widget is suitable or perfect for multiline
blocks of text.
Python development using grid layout manager means dividing the windows into row and columns. Here is the grid layout
row and column with its values for general understanding before going through program.
Python GUI Password Protected Program
This gui password protected program using python, contains event handling code, that you will learn about it in next
tutorial.
In this example, user ask to enter their password to grant access. If he/she will enter the correct password
(here codescracker), then he/she is the authorised person, or if he/she will enter the wrong password, then he/she isn't
authorised person.
Now let's type any password say forgotten as shown in the following screenshot.
Now press the Login button to watch the output as shown below.
Now type the correct password, that is codescracker and press login to see the following output.