Tkinter là một thư viện Python được sử dụng để tạo và phát triển các ứng dụng dựa trên GUI.
- Để hiển thị / hiển thị tiện ích con, hãy sử dụng pack () quản lý hình học
- Để ẩn bất kỳ tiện ích con nào khỏi ứng dụng, hãy sử dụng pack_forget () phương pháp.
Ví dụ
Hãy lấy ví dụ này để hiểu cách hiển thị / ẩn tiện ích con -
# 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() Đầu ra
Chạy đoạn mã trên sẽ mở ra một cửa sổ có Nút để hiển thị / ẩn các tiện ích khỏi ứng dụng.
Bây giờ, hãy nhấp vào nút để hiển thị / ẩn văn bản nhãn khỏi cửa sổ.