1.Creating our first Python GUI:
Python is a very powerful programming language. It ships with the built-in Tkinter module. In only a few lines of code.we can build our first Python GUI.
How to do it…. Here are the four lines of Python code required to create the resulting GUI: import tkinter as tk win=tk.TK() win.title("Python GUI") win.mainloop()
2.Preventing the GUI from being resized:
This recipe extends the previous one. Therefore, It is necessary to have typed Recipe1 yourself into a project of your own or downloaded the code from
We are preventing the GUI from being resized: import tkinter as tk win=tk.TK() win.title("Python GUI") win.resizable(0,0) win.mainloop()
3.Creating a button and changing their text property:
This recipe extends the previous one. You can download the entire code from the Packt Publishing website:
Modify Adding A Label
aLable=ttk.Lable(win,text="A Label") aLabel.grid(column=0,row=0)
Button Click Event Callback Function
Adding A Button
action=ttk.Button(win,text="Click Me!",command=clickme) action.grid(column=1,row=0)
4.Using Scrolled Text widget:
ScrolledText widget is much larger than a simple Entry widget and spans multiple lines. They are widget like Notepad and wrap lines, automatically enabling vertical scrollbars when the text gets larger than the height of the ScrolledText widget.
Add This Import To The Top Of The Python Module
From tkinter import scrolledtext
Using A Scrolled Text Control
Using padding to add space around the widget:
Our GUI is being created nicely. Next, we will improve the visual aspects of our widgets by adding a little space around them
The procedural way of adding spacing around widgets is shown first and then we will use a loop to achieve the same thing in a much better way. Our LabelFrame looks a bit tight as it blends into the main window towards the bottom.
We can use a loop to add space around the labels contained within the LabelFrame:
self.canvas_l = Label(self.master, text=”choose a color:”, font=”helvetica 12″) self.canvas_l.grid(row=9, column=1, sticky=S, ipady=30)