Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在for循环中创建时替代设置高度/宽度(看起来太大)的按钮_Python_Button_Tkinter - Fatal编程技术网

Python 在for循环中创建时替代设置高度/宽度(看起来太大)的按钮

Python 在for循环中创建时替代设置高度/宽度(看起来太大)的按钮,python,button,tkinter,Python,Button,Tkinter,我有一个创建按钮数组的方法(模拟网格)。用户可以单击按钮,并在其上显示“标记”(图像)。我的问题是,即使我在我的方法中设置了高度和宽度,当它被调用时,按钮看起来太大了。重要的是要注意,当用户单击按钮时,它会“收缩”到我指定的大小。。。奇怪的以下是我的方法: def create_grid(self, leng) : try: global board board = grid.Grid(leng) frame = tkinter.Fra

我有一个创建按钮数组的方法(模拟网格)。用户可以单击按钮,并在其上显示“标记”(图像)。我的问题是,即使我在我的方法中设置了高度和宽度,当它被调用时,按钮看起来太大了。重要的是要注意,当用户单击按钮时,它会“收缩”到我指定的大小。。。奇怪的以下是我的方法:

def create_grid(self, leng) :
    try:
        global board
        board = grid.Grid(leng)


        frame = tkinter.Frame(self.main_window)
        buttons = []
        for row_index in range(0,leng) :
            row = []
            for column_index in range(0,leng) :
                button = tkinter.Button(frame, height=36, \
                    width = 36, text = "  ", \
                    command=lambda row=row_index, \
                        column=column_index: \
                        self.button_clicked(row,column))
                button.grid(row=row_index, column=column_index)
                row.append(button)
            buttons.append(row)

        self.gomuko_buttons = buttons
        return frame.grid(row=0, column=0)
    except TypeError:
        return

此外,按下按钮时执行的功能如下:

def button_clicked(self,row,column) :
    global turns
    grid.Comp = False
    board.place_marker(row, column)
    button = self.gomuko_buttons[row][column]


    button['image'] = self.photo1
    button['state'] = 'disabled'
    turns = turns + 1
button = tkinter.Button(frame, height=0, \
                    width = 0, text = " ", image = self.blank, \
                    command=lambda row=row_index, \
                        column=column_index: \
                        self.button_clicked(row,column))
有什么提示吗(

编辑:

由于Bryan Oakley的回答,我解决了这个问题。结果表明,图像和文本的宽度和高度行为不同。我通过简单地向循环中添加一个空白图像来解决这个问题,如下所示:

def button_clicked(self,row,column) :
    global turns
    grid.Comp = False
    board.place_marker(row, column)
    button = self.gomuko_buttons[row][column]


    button['image'] = self.photo1
    button['state'] = 'disabled'
    turns = turns + 1
button = tkinter.Button(frame, height=0, \
                    width = 0, text = " ", image = self.blank, \
                    command=lambda row=row_index, \
                        column=column_index: \
                        self.button_clicked(row,column))
谢谢!

我的猜测是,你认为你给了按钮36像素的宽度,但实际上你给了它36个字符的宽度。在你的回调中,我猜你给了按钮一个图像,这会导致按钮缩小

在Tkinter中,根据按钮是图像还是文本,宽度选项的解释会有所不同。如果按钮只有文本,则以“平均大小字符”(实际上是0(零)的宽度)指定宽度。如果给按钮一个图像,则宽度选项解释为像素


此行为已记录在案。请参见,例如,

问题还有很多,您正在告诉我们。例如,您的代码显示您正在创建一个带有空白标签的按钮,但图片显示的是一个带有图像的按钮。您是否在某个时刻给按钮一个图像?肯定没有看到……是的,当单击按钮时,会显示一个图像e将出现。我将把这段代码添加到帖子中。谢谢!谢谢你指出这一点!在我的循环中,我添加了一个空白图像,并将w,h设置为0(因此它会自行计算)。