Tkinter:按行和列标识按钮

时间:2015-02-26 19:16:48

标签: python python-3.x tkinter

我希望能够根据网格上的行和列选择一个按钮,然后按钮并控制其Text and Relief。我还没有能够以这种方式在小部件或单元上找到任何东西。 编辑: 我改变了根放置的地方,现在它说我不能使用我收到的一个元组来解决问题'这是有道理的,我需要访问小部件本身。任何建议

import tkinter
import functools
import random
from time import sleep
width = input('Enter the grid width. ')
height = input('Enter the grid height. ')
numb = input('Enter the number of bombs. ')
Matrix = [[0 for lp in range(int(width))] for fg in range(int(height))]
def ranintx():
    return  random.randint(0,int(width))
def raninty():
    return random.randint(0,int(height))

def placemines():
   y = ranintx()
   x = raninty()
   for ranintformine in range(int(numb)):
       x = ranintx()
       y = raninty()
       Matrix[y-1][x-1] = 1
placemines()
def sunken(event, self, x, y):
    button = event.widget
    button['relief'] = 'sunken'
    if x - 1 < 0 :
        return
    if x > int(width) + 1 :
        return
    if y - 1 < 0 :
        return
    if y > int(height) + 1 :
        return
    if Matrix[x][y] == 1 :
        top = tkinter.Toplevel()
        top.title("About this application...")

        msg = tkinter.Message(top, text="You Lose")
        msg.pack()

        button = tkinter.Button(top, text="Dismiss", command=top.destroy)
        button.pack()
        print('Column = {}\nRow = {}'.format(x, y))
    else:
       n1 = x - 1
       n2 = y - 1

       for lp in range(3):
            for lp2 in range(3):
                abutton = root.grid_location(n1, n2)
                abutton['relief'] = ['sunken']
                # I want to be able to change and select the button here. This was one of my poor attempt 
                n2 =+ 1
            n1 =+ 1
def push(event, self, x, y):
    button = event.widget
    if Matrix[x][y] == 1 :
         print('Column = {}\nRow = {}'.format(x, y))
 class MineSweep(tkinter.Frame):

        @classmethod
        def main(cls, width, height):
        window = cls(root, width, height)
        '''placemine()'''
        root.mainloop()

    def __init__(self, master, width, height):
        super().__init__(master)
        self.__width = width
        self.__height = height
        self.__build_buttons()
        self.grid()
    #def sunken(event):
    #    button = event.widget
    #    button['relief'] = 'sunken'
    def __build_buttons(self):
        self.__buttons = []
        for y in range(self.__height):
            row = []
            for x in range(self.__width):
                button = tkinter.Button(self, state='disabled')
                button.grid(column=x, row=y)
                button['text'] = ' '
                print(grid.slaves)
                self.checked = True
                #button['command'] = functools.partial(self.__push, x, y)
                button.bind("<Button-3>",
                    lambda event, arg=x, brg=y: push(event, self, arg, brg))
                button['relief'] = 'raised'
                button.bind("<Button-1>",
                    lambda event, arg=x, brg=y: sunken(event, self, arg, brg))


                #button['command'] = sunken
                row.append(button)
             self.__buttons.append(row)



root = tkinter.Tk()
if __name__ == '__main__':
    MineSweep.main(int(width), int(height))

1 个答案:

答案 0 :(得分:0)

你的程序有些问题。首先,sunken应该是该类的一种方法。将它放在课外是非常奇怪的,然后你传递self作为其他论点。它有效,但它使代码非常混乱。

话虽这么说,你实际上非常接近于完成这项工作。您已经在列表列表中保存了对每个按钮的引用,因此您应该能够使用self.__buttons[y][x]获取窗口小部件。 但是,因为sunken不是该类的一部分,并且因为您使用两个下划线命名该变量,所以sunken函数无法访问该变量。

如果将变量更改为具有单个下划线而不是double,则代码应该或多或少地完全按原样运行(一旦修复了语法和缩进错误)。另一个解决方案是在类上创建sunken方法并修复你如何调用它(删除自身参数,将其称为self.sunken),它将使用两个下划线。

坦率地说,使用两个下划线实际上没有任何好处。避免使用它的诱惑。至少,在基本逻辑工作之前不要使用它,然后你可以返回并隐藏你不想暴露的属性。