Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Chạy nhiều lệnh khi nhấn nút trong Tkinter

Button có thể thực hiện nhiều hành động bằng lambda list: command=lambda: [func1(), func2()].

Ví dụ

# Import Tkinter
from tkinter import *

# Tạo khung
win = Tk()
win.geometry("700x350")

# Các hàm
def display_msg():
    label.config(text="Top List of Programming Language")

def show_list():
    listbox = Listbox(win, height=10, width=15, bg='grey', activestyle='dotbox', font='aerial')
    listbox.insert(1, "Go")
    listbox.insert(1, "Java")
    listbox.insert(1, "Python")
    listbox.insert(1, "C++")
    listbox.insert(1, "Ruby")
    listbox.pack()
    button.destroy()

# Label
label = Label(win, text="", font=('aerial 18 bold'))
label.pack(pady=20)

# Nút nhiều lệnh
button = Button(win, text="Click Here", command=lambda: [display_msg(), show_list()])
button.pack()

win.mainloop()

Kết quả

Click nút thực hiện 2 tác vụ: hiển thị Label và Listbox, xóa nút.

Chạy nhiều lệnh khi nhấn nút trong Tkinter

Chạy nhiều lệnh khi nhấn nút trong Tkinter