【问题标题】:Mysterious extra space after buttons in tkintertkinter 中按钮后的神秘额外空间
【发布时间】:2017-12-13 17:58:29
【问题描述】:

我在 Windows 上的 Python3.4 上运行 tkinter,我希望我的 GUI 框中有两个按钮。 我关注[this link]

代码是这样的:

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.initialize()

    def initialize(self):
        button_crop = tk.Button(self, text=u"Crop", command=self.OnCrop)
        button_crop.pack(side="left")

        button_reset = tk.Button(self, text=u"Reset", command=self.OnReset)
        button_reset.pack(side="left")

    def OnCrop(self):
        pass

    def OnReset(self):
        pass

app = App()

app.mainloop() 

现在我得到了一个右侧有一些额外空间的按钮

我尝试初始化 grid() 然后 button_crop.grid(column=0, row=1) 但我得到了相同的结果。

请帮我删除右侧多余的空白。

【问题讨论】:

  • 尝试删除最小和最大按钮。似乎表单不能小于系统按钮的大小。看这里stackoverflow.com/questions/2969870/…
  • @ventik 那是不可取的。我可以增加按钮的大小以适应最小宽度。
  • 您希望按钮增大以填充空间,还是缩小窗口以移除多余的空间?

标签: python python-3.x tkinter tkinter-canvas


【解决方案1】:

你想要这种行为吗?

import tkinter as tk

class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.initialize()

    def initialize(self):        

        button_crop = tk.Button(self, text=u"Crop", command=self.OnCrop)
        button_crop.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))

        button_crop = tk.Button(self, text=u"Reset", command=self.OnReset)
        button_crop.grid(row=0, column=1, sticky=(tk.N, tk.S, tk.E, tk.W))

        for i in range(2):
            self.columnconfigure(i, weight=1)

        self.rowconfigure(0, weight=1)

    def OnCrop(self):
        pass

    def OnReset(self):
        pass

app = App()

app.mainloop() 

【讨论】:

  • 我能不能只扩展“裁剪”和“重置”按钮的大小,直到它们都加起来达到最小宽度?
  • 是的,这很棒!您能否解释一下您的代码中的哪些更改帮助我获得了这种行为?非常感谢。
  • columnconfigure 为某些列设置选项,weight=1 表示该列可伸缩。在这里阅读更多infohost.nmt.edu/tcc/help/pubs/tkinter/web/grid-config.html
  • 感谢@ventik 的帮助
猜你喜欢
  • 1970-01-01
  • 2018-02-26
  • 2010-10-08
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 2013-01-21
  • 2013-02-01
  • 1970-01-01
相关资源
最近更新 更多