繁体   English   中英

Tkinter / Python-在调整窗口大小时禁用按钮的大小调整

[英]Tkinter / Python - disabling resizing of buttons when a window is resized

我有一个带有列表框的窗口,该列表框相互调整大小。 我为此使用网格。 列表框下方有一排按钮,这些按钮当前正在移动并随窗口调整大小。 我希望按钮在窗口和列表框调整大小时保持不变,并保持一致的大小(因此,按钮始终保持在列表框的左下角)。

是否有捷径可寻? 我今天才刚开始使用Tkinter,所以我可能只是在文档中错过了它,或者可能对网格布局感到困惑。

按钮设置如下:

nextbutton = Button(self, width = 30, height = 30, image = ffbtn)
nextbutton.image = ffbtn
nextbutton.bind("<Button-1>", self.forward)
nextbutton.grid(row = 10, column=4, sticky='w')

我已经尝试了所有粘性组合,但是那些不调整按钮大小的组合仍然允许它们随意移动,这是我不希望的。

编辑:这是我所要求的非常基本的示例。 调整窗口大小时,按钮彼此分开。 我不要

顺便说一句,即使我使用一排或一排的列/行跨占用了所有空间,我是否也需要在最后配置所有这些网格来设置权重?

import tkinter
from tkinter import *
from tkinter import ttk

class Application(tkinter.Tk):
    def __init__(self, parent):
        tkinter.Tk.__init__(self, parent)
        self.minsize(300,300)
        self.parent = parent
        self.main()

    def main(self):

        self.grid()

        listbox = tkinter.Listbox(self, width=20, height=25, bg = 'grey')
        listbox.grid(padx=30, pady=30, columnspan=11,  sticky='NEW')

        bt1 = Button(self, width = 5, height = 5)
        bt1.grid(row=10, column=0, padx=(30,0), sticky='w')

        bt2 = Button(self, width = 5, height = 5)
        bt2.grid(row = 10, column=1, sticky='w')

        bt3 = Button(self, width = 5, height = 5)
        bt3.grid(row = 10, column=2, sticky='w')

        bt4 = Button(self, width = 5, height = 5) 
        bt4.grid(row = 10, column=3, sticky='w')

        bt5 = Button(self, width = 5, height = 5)
        bt5.grid(row = 10, column=4, sticky='w')

        self.grid_columnconfigure(0,weight=1)
        self.grid_columnconfigure(1,weight=1)
        self.grid_columnconfigure(2,weight=1)
        self.grid_columnconfigure(3,weight=1)
        self.grid_columnconfigure(4,weight=1)
        self.grid_columnconfigure(5,weight=1)
        self.grid_columnconfigure(6,weight=1)
        self.grid_columnconfigure(7,weight=1)
        self.grid_columnconfigure(8,weight=1)
        self.grid_columnconfigure(9,weight=1)
        self.grid_columnconfigure(10,weight=1)      
        self.grid_rowconfigure(0,weight=1)
        self.grid_rowconfigure(1,weight=1)
        self.grid_rowconfigure(2,weight=1)
        self.grid_rowconfigure(3,weight=1)
        self.grid_rowconfigure(4,weight=1)
        self.grid_rowconfigure(5,weight=1)
        self.grid_rowconfigure(6,weight=1)
        self.grid_rowconfigure(7,weight=1)
        self.grid_rowconfigure(8,weight=1)
        self.grid_rowconfigure(9,weight=1)
        self.grid_rowconfigure(10,weight=1)

app = Application(None)
app.mainloop()

通过为网格中的所有行和列赋予非零权重 ,您可以明确地使它们在主窗口中调整大小。

由于只希望调整列表框的大小,因此仅对包含列表框的行和列之一赋予非零权重:

def main(self):
    ...
    # The default weight of all rows/columns is 0. Only modify it for row 0 and column 10
    self.grid_rowconfigure(0, weight=1)
    self.grid_columnconfigure(10, weight=1)

有关更多信息,请查看packgridplace方法的文档。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM