tkinter中一行代码中的多个按钮 [英] multiple buttons in one line of code in tkinter

查看:61
本文介绍了tkinter中一行代码中的多个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建座位选择屏幕时,我需要在Tkinter中创建200个检查按钮.有没有一种方法可以创建所有这些按钮,而不必像在pygame中那样逐行键入它们?到目前为止,我已经尝试过了,但是似乎没有用.(我不是超级聪明的编码器,所以这可能是一个愚蠢的错误.)

I need to create 200 check buttons in Tkinter, as I'm creating a seat selection screen. Is there a way of creating all of these buttons without having to type them out line by line, like you can in pygame? so far I've tried this but it doesn't seem to work. (I am not a super smart coder so it may be a silly mistake).


root = tk.Tk()

frame1 = tk.Frame(root)
frame1.pack(side=tk.TOP, fill=tk.X)
button = list()
for i in range(4):
button.append(tk.Button(frame1, image=karirano, command=partial(klik, i)))
    button[-1].grid(row=0,column=i)
root.mainloop()```

推荐答案

是的,您可以在循环中创建按钮.如果要绘制座位表,我将首先使用简单的数据结构定义外观,让您轻松查看布局.

Yes, you can create buttons in a loop. If I were doing a seating chart, I would start by defining what it looks like with a simple data structure that lets you easily visualize the layout.

例如:

rows = {
    "a": "  xxxx xxxxxxxx xxxx",
    "b": "xxxxxx xxxxxxxx xxxxxx",
    "c": "xxxxxx xxxxxxxx xxxxxx",
}

然后您可以遍历此数据,在有"x"的位置放置一个座位.

You can then iterate over this data, placing a seat everywhere there is an "x".

例如:

for row_number, row_letter in enumerate(rows.keys()):
    for seat_number, c in enumerate(rows[row_letter]):
        if c == "x":
            seat = tk.Button(frame1, ...)
            seat.grid(row=row_number, column=seat_number)

这篇关于tkinter中一行代码中的多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆