Tkinter网格管理器切断按钮

时间:2015-07-01 17:51:27

标签: python-2.7 tkinter

我有以下布局:

    btnReset = Button(self.upperButtonFrame, text = "Reset", width = 12, command=self.__parent.reset)
    btnReset.grid(row=0, column=0, sticky="W", pady=5)
    btnRender = Button(self.upperButtonFrame, text = "Render", width = 9, command = self.render)
    btnRender.grid(row=0, column=1, pady=5)

    self.bScattered = Radiobutton(self.upperButtonFrame, text="Backscattered", variable=self.plotType, value=1).grid(row=1, column=0, sticky="W")
    self.depolarized = Radiobutton(self.upperButtonFrame, text="Depolarized", variable=self.plotType, value=2).grid(row=2, column=0, sticky="W")

    self.rng = Label(self.upperButtonFrame, text="Step").grid(row=3, column=0, sticky="W")
    self.e = Entry(self.upperButtonFrame).grid(row=3,column=1)
    self.to = Label(self.upperButtonFrame, text="to").grid(row=3, column=2)
    self.e2 = Entry(self.upperButtonFrame).grid(row=3,column=3)

问题是我的第三行看起来像:

enter image description here

而不是均匀地隔开我试图放置的4个小部件。如何修复网格管理器切断最后两个并在屏幕上均匀分隔它们?

编辑:这是我的问题的一个最小例子,运行它应该重现上面显示的结果

from Tkinter import Label, Toplevel, Frame, Button, IntVar, \
   BOTH, BOTTOM, Radiobutton, Entry, TOP, Tk

CHILDWIDTH      = 200
CHILDHEIGHT     = 325

class ToolsWindow(Toplevel):

    def __init__(self, root):
        Toplevel.__init__(self, root)
        self.__root = root
        self.plotType = IntVar()

        self.title("Tools")
        self.geometry('%dx%d+%d+%d' % (CHILDWIDTH, CHILDHEIGHT,0, 0))
        #self.resizable(width=FALSE, height=FALSE)
        self.protocol("WM_DELETE_WINDOW", ToolsWindow.ignore)
        self.container = Frame(self, background="red")
        self.container.pack(side=TOP, fill=BOTH, expand=True )    

        self.coordinateFrame = Frame(self.container, background="green", width=50, height=50)
        self.coordinateFrame.config(highlightthickness=1)                        # create a small border around the frame
        self.coordinateFrame.config(highlightbackground="grey")
        self.coordinateFrame.pack(side=BOTTOM, fill=BOTH, expand=False)                                      

    @staticmethod
    def ignore():
        pass

    def setupToolBarButtons(self):
        self.upperButtonFrame = Frame(self.container, background="blue")                                  # upper button frame holding text buttons
        self.upperButtonFrame.pack(side=TOP)    

        btnReset = Button(self.upperButtonFrame, text = "Reset", width = 12, command=self.render)
        btnReset.grid(row=0, column=0, sticky="w")
        btnRender = Button(self.upperButtonFrame, text = "Render", width = 9, command = self.render)
        btnRender.grid(row=0, column=1, sticky="w")

        self.bScattered = Radiobutton(self.upperButtonFrame, text="Backscattered", 
            variable=self.plotType, value=1).grid(row=1, column=0, sticky="w")
        self.depolarized = Radiobutton(self.upperButtonFrame, text="Depolarized", 
            variable=self.plotType, value=2).grid(row=2, column=0, sticky="w")


        self.rng = Label(self.upperButtonFrame, text="Step")
        self.rng.grid(row=3, column=0, sticky="w")
        self.e = Entry(self.upperButtonFrame, width=8)
        self.e.grid(row=3, column=1, sticky="w")

        self.to = Label(self.upperButtonFrame, text="to")
        self.to.grid(row=3, column=2, sticky="w")
        self.e2 = Entry(self.upperButtonFrame, width=8)
        self.e2.grid(row=3, column=3, sticky="w")

    def render(self):
        pass

root = Tk()
tool = ToolsWindow(root)
tool.setupToolBarButtons()
root.mainloop()

1 个答案:

答案 0 :(得分:0)

下面发布的稍加修改的程序会正确呈现,因此它必须是您未发布导致问题的代码中的内容。您是否在任何地方限制根窗口的几何尺寸?我不知道“均匀间隔”是什么意思,但你可以在大多数小部件上使用width参数来增加大小。

from Tkinter import *

class TestIt(object):
    def __init__(self, top):
        self.upperButtonFrame=top
        btnReset = Button(self.upperButtonFrame, text = "Reset", width = 12)
        btnReset.grid(row=0, column=0, sticky="W", pady=5)
        btnRender = Button(self.upperButtonFrame, text = "Render", width = 9)
        btnRender.grid(row=0, column=1, pady=5)

        self.bScattered = Radiobutton(self.upperButtonFrame, text="Backscattered").grid(row=1, column=0
        self.depolarized = Radiobutton(self.upperButtonFrame, text="Depolarized").grid(row=2, column=0,

        self.rng = Label(self.upperButtonFrame, text="Step").grid(row=3, column=0, sticky="e")
        self.e = Entry(self.upperButtonFrame).grid(row=3,column=1)
        self.to = Label(self.upperButtonFrame, text="to").grid(row=3,   column=2, sticky="e")
        self.e2 = Entry(self.upperButtonFrame).grid(row=3,column=3)

top = Tk()
TI=TestIt(top)
top.mainloop()