Python动态添加按钮单独管理

时间:2018-04-12 10:00:33

标签: python tkinter

我正在创建一个简单的TicTacToe GUI。这是我到目前为止所得到的:

import tkinter as tk  

def initTable(master, rows, columns):
    for i in range(0, columns):
        button = tk.Button(master, text="", height=1, width=2)
        button.grid(row=i)
        for j in range(0, rows):
            button2 = tk.Button(master, text="", height=1, width=2)
            button2.grid(row=i, column=j)


if "__main__" == __name__:
    print("Welcome to the TicTacToe game!")
    print("Recommended size: 10x10")
    correctinput = False
    while True:
        if correctinput:
            print("The entered size is too big, please enter a smaller value")
        height = int(input("Please enter the height (Max. 70)!"))
        width = int(input("Please enter the width (Max. 70)!"))
        correctinput = True
        if height <= 70 and width <= 70:  # This check is needed because it seems that you can't use any bigger
            # table than this, it will freeze the computer...
            break
    master = tk.Tk()
    master.resizable(width=False, height=False)
    initTable(master, height, width)
    master.mainloop()

因此,这将创建具有用户指定的宽度和高度的GUI。但是,现在我想管理这些单独创建的按钮。 例如,如果在GUI中创建的按钮上按下鼠标左键,则应在该标签中显示X. 我找到了这些来显示它:

def leftclick(event):
    print("leftclick")


def rightclick(event):
    print("rightclick")

button.bind('<Button-1>', leftclick)
button.bind('<Button-3>', rightclick)

但是我不知道如何使用它,因为按钮没有唯一的名称等等...也许用winfo_child()?

1 个答案:

答案 0 :(得分:0)

您要做的第一件事就是修复initTable功能。如果仔细观察一下,你会发现它创建了太多按钮:

def initTable(master, rows, columns):
    for i in range(0, columns):
        button = tk.Button(master, text="", height=1, width=2)
        button.grid(row=i)
        # ^ we don't need that button
        for j in range(0, rows):
            button2 = tk.Button(master, text="", height=1, width=2)
            button2.grid(row=i, column=j)

嵌套循环实际上是在彼此的顶部创建两个分层按钮。正确的代码仅在内循环中创建按钮:

def initTable(master, rows, columns):
    for i in range(0, columns):
        for j in range(0, rows):
            button = tk.Button(master, text="", height=1, width=2)
            button.grid(row=i, column=j)

如果你正在使用它,你还应该将leftclickrightclick功能连接到每个创建的按钮:

def initTable(master, rows, columns):
    for i in range(0, columns):
        for j in range(0, rows):
            button = tk.Button(master, text="", height=1, width=2)
            button.grid(row=i, column=j)
            button.bind('<Button-1>', leftclick)
            button.bind('<Button-3>', rightclick)

回调函数可以访问通过event.widget属性触发事件的按钮,因此实现非常简单:

def leftclick(event):
    mark_button(event.widget, 'X')

def rightclick(event):
    mark_button(event.widget, 'O')

def mark_button(button, mark):
    # if the button has already been clicked, do nothing
    if button['text']:
        return

    button['text'] = mark