如何在 Tkinter 中显示和隐藏小组件?
Tkinter 是一个用于创建和开发基于 GUI 的应用程序的 Python 库。假设我们必须创建一个应用程序,以便我们可以显示或隐藏小组件。
- 要显示小组件,请使用**pack()**几何管理器
- 要从应用程序中隐藏任何小组件,请使用**pack_forget()**方法。
示例
让我们通过下面的例子来理解如何显示/隐藏小组件 −
# Import the required libraries
from tkinter import *
from tkinter import ttk
# Create an instance of tkinter frame
win = Tk()
# Set the size of the tkinter window
win.geometry("700x350")
# Define the style for combobox widget
style = ttk.Style()
style.theme_use('xpnative')
# Define a function to show/hide widget
def show_widget():
label.pack()
def hide_widget():
label.pack_forget()
b1.configure(text="Show", command=show_widget)
# Add a label widget
label = ttk.Label(win, text="Eat, Sleep, Code and Repeat", font=('Aerial 11'))
label.pack(pady=30)
# Add a Button widget
b1 = ttk.Button(win, text="Hide", command=hide_widget)
b1.pack(pady=20)
win.mainloop()输出
运行上述代码将打开一个窗口,其中有一个按钮用于显示/隐藏应用程序中的小组件。
现在,单击按钮以显示/隐藏窗口中的标签文本。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP