个性化阅读
专注于IT技术分析

Python Tkinter 创建LabelFrame并向其添加小部件

Tkinter是一个Python模块, 用于创建GUI(图形用户界面)应用程序。它是Python附带的广泛使用的模块。它由各种类型的小部件组成, 这些小部件可用于使GUI更加用户友好和有吸引力, 并且可以增加功能。

LabelFrame可以如下创建:

-> import tkinter
-> create root
-> create LabelFrame as child of root
label_frame = ttk.LabelFrame(parent, value = options, ...)

代码1:创建LabelFrame并向其中添加一条消息。

# Import only those methods
# which are mentioned below, this way of
# importing methods is efficient
from tkinter import Tk, mainloop, LEFT, TOP
from tkinter.ttk import *
  
# Creating tkinter window with fixed geometry
root = Tk()
root.geometry( '250x150' )
  
# This will create a LabelFrame
label_frame = LabelFrame(root, text = 'This is Label Frame' )
label_frame.pack(expand = 'yes' , fill = 'both' )
  
label1 = Label(label_frame, text = '1. This is a Label.' )
label1.place(x = 0 , y = 5 )
  
label2 = Label(label_frame, text = '2. This is another Label.' )
label2.place(x = 0 , y = 35 )
  
label3 = Label(label_frame, text = '3. We can add multiple\n    widgets in it.' )
  
label3.place(x = 0 , y = 65 )
  
# This creates an infinite loop which generally
# waits for any interrupt (like keyboard or
# mouse) to terminate
mainloop()

输出如下:

Python Tkinter |创建LabelFrame并向其添加小部件1

代码2:在LabelFrame中添加Button和CheckButton小部件。

# Import only those methods
# which are mentioned below, this way of
# importing methods is efficient
from tkinter import Tk, mainloop, LEFT, TOP
from tkinter.ttk import *
  
# Creating tkinter window with fixed geometry
root = Tk()
root.geometry( '250x150' )
  
# This will create a LabelFrame
label_frame = LabelFrame(root, text = 'This is Label Frame' )
label_frame.pack(expand = 'yes' , fill = 'both' )
  
# Buttons
btn1 = Button(label_frame, text = 'Button 1' )
btn1.place(x = 30 , y = 10 )
btn2 = Button(label_frame, text = 'Button 2' )
btn2.place(x = 130 , y = 10 )
  
# Checkbuttons
chkbtn1 = Checkbutton(label_frame, text = 'Checkbutton 1' )
chkbtn1.place(x = 30 , y = 50 )
chkbtn2 = Checkbutton(label_frame, text = 'Checkbutton 2' )
chkbtn2.place(x = 30 , y = 80 )
  
# This creates infinite loop which generally
# waits for any interrupt (like keyboard or
# mouse) to terminate
mainloop()

输出如下:

Python Tkinter |创建LabelFrame并向其添加小部件2

注意:一个也可以添加另一个标签框在另一个里面LabelFrame, 以及任何人都可以做的样式标签框就像我们做其他小部件的样式一样。

首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。


赞(0)
未经允许不得转载:srcmini » Python Tkinter 创建LabelFrame并向其添加小部件

评论 抢沙发

评论前必须登录!