【问题标题】:Tkinter: Identifying button by row and columnTkinter:按行和列识别按钮
【发布时间】:2015-02-26 19:16:47
【问题描述】:

我希望能够根据它在网格上的行和列以及按钮来选择按钮并控制其文本和浮雕。我无法在以这种方式使用的小部件或单元格上找到任何东西。 编辑: 我更改了根的放置位置,现在它说我不能使用我收到的“救济”元组,这是有道理的,我需要访问小部件本身。任何建议

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))

【问题讨论】:

  • 你的尝试有什么问题?
  • 您的代码缩进不当。另外,您需要向我们展示您是如何调用此函数的。请给出一个说明问题的完整程序。

标签: python python-3.x tkinter


【解决方案1】:

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

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

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

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

【讨论】:

  • 谢谢你,我会试试你说的
猜你喜欢
  • 2021-12-22
  • 1970-01-01
  • 2014-08-06
  • 2021-08-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-09
  • 2021-08-04
  • 1970-01-01
相关资源
最近更新 更多