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

Cách tạo Button động (dynamically) trong Tkinter?

Tạo button động nghĩa là tùy chỉnh button và chức năng bằng cách thêm sự kiện.

Import tkinter, dùng Button(parent, textvariable, command) trong vòng lặp.

Ví dụ

from tkinter import *
import tkinter as tk

win = tk.Tk()
win.geometry("700x200")
win.title("www.tutorialspoint.com")

n = 10  # số button
i = 3   # hàng

for j in range(n):
    mybutton = Button(win, text=j)
    mybutton.grid(row=i, column=j)

win.mainloop()

Kết quả

Chạy code tạo 10 button hàng ngang hàng 3.

Cách tạo Button động (dynamically) trong Tkinter?