In this Python TKinter article i want to show you Creating Label Frame. also if you
are interested in Python GUI Development with different libraries, you can read the tutorials
in the below links.
Also you can read more articles on Python GUI Development
1: PyQt5 GUI Development Tutorials
2: Pyside2 GUI Development Tutorials
3: wxPython GUI Development Tutorials
4: Kivy GUI Development Tutorials
This is the complete source code for this article
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import tkinter as tk from tkinter import ttk class App(tk.Tk): def __init__(self): super(App, self).__init__() self.title("Tkinter Label Frame") self.minsize(600,400) self.wm_iconbitmap('icon.ico') self.labelFrame = ttk.LabelFrame(self, text = "Label Frame") self.labelFrame.grid(column = 0, row = 7, padx = 20, pady = 40) self.labels() def labels(self): ttk.Label(self.labelFrame, text = "Label One" ).grid(column = 0, row = 0, sticky = tk.W) ttk.Label(self.labelFrame, text = "Label Two").grid(column = 0, row = 1,sticky = tk.W) ttk.Label(self.labelFrame, text = "Label Three").grid(column = 0, row = 2, sticky = tk.W) app = App() app.mainloop() |
These are the imports that we need for this article, basically it is tkinter library, also we are
going to import ttk from tkinter.
1 2 |
import tkinter as tk from tkinter import ttk |
So in this part we have used Object Oriented Programming (OOP), we have created a class that extends from tk.TKand we have added our window title, window size and window icon in the constructor of the class, make sure that you have added an icon to your working directory . also we have called our def labelsl() method in this class. we need to also create a label frame in here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class App(tk.Tk): def __init__(self): super(App, self).__init__() self.title("Tkinter Label Frame") self.minsize(600,400) self.wm_iconbitmap('icon.ico') self.labelFrame = ttk.LabelFrame(self, text = "Label Frame") self.labelFrame.grid(column = 0, row = 7, padx = 20, pady = 40) self.labels() |
In this method we have created three labels and we have added those labels in our LabelFrame.
1 2 3 4 |
def labels(self): ttk.Label(self.labelFrame, text = "Label One" ).grid(column = 0, row = 0, sticky = tk.W) ttk.Label(self.labelFrame, text = "Label Two").grid(column = 0, row = 1,sticky = tk.W) ttk.Label(self.labelFrame, text = "Label Three").grid(column = 0, row = 2, sticky = tk.W) |
Run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email