在 Tkinter 画布中创建 LabelFrame
在本文中,我们将讨论如何在 Tkinter 画布中创建 LabelFrame。
句法:
canvas = Canvas(app, bg=”#Colour of canvas”, height=#Height of canvas, width=#Width of canvas)
label_frame = LabelFrame(canvas, text=”#Text which you want to show in LabelFrame”)
label = Label(label_frame, text=”#Text which you want to show inside label”)
canvas.create_window(#Distance from x-axis, #Distance from y-axis, window=label_frame, anchor=’w’)
逐步实施
步骤 1:首先,导入库 Tkinter。
from tkinter import *
第 2 步:现在,使用 Tkinter 创建并调整 GUI 应用程序的大小。
app = Tk()
app.geometry(“500×500”) #Give the size of app you want
第 3 步:然后,在 GUI 应用程序中创建一个画布。
canvas = Canvas(app, bg=”#Colour of canvas”, height=#Height of canvas, width=#Width of canvas)
canvas.pack()
第 4 步:进一步,在画布内创建并显示 LabelFrame。
label_frame = LabelFrame(canvas, text=”#Text which you want to show in LabelFrame” )
第 5 步:此外,在创建的 LabelFrame 内创建一个标签。
label = Label(label_frame, text=”#Text which you want to show in label”)
label.pack()
第6步:稍后,在画布内定位并显示画布和LabelFrame。
canvas.create_window(#Distance from x-axis, #Distance from y-axis, window=label_frame, anchor=’w’)
第 7 步:最后,制作用于在屏幕上显示 GUI 应用程序的循环
app.mainloop()
例子:
在此示例中,包含文本“数据结构”的 LabelFrame 显示在包含文本“ Geeks For Geeks”的画布中。 '
Python3
# Python program to create a
# LabelFrame inside a Tkinter canvas
# Import the library tkinter
from tkinter import *
# Create and resizing a GUI app
app = Tk()
app.geometry("500x500")
# Creating and displaying a canvas
canvas = Canvas(app, bg="yellow", height=200, width=300)
canvas.pack()
# Creating and displaying a LabelFrame
label_frame = LabelFrame(canvas, text="Geeks For Geeks")
label = Label(label_frame, text="Data Structures")
label.pack()
# Displaying and resizing of LabelFrame inside Canvas
canvas.create_window(100, 100, window=label_frame, anchor='w')
# Make the infinite loop for displaying the app
app.mainloop()
输出: