在框架上居中使用小部件

时间:2018-02-01 17:39:59

标签: python tkinter frame

我的小部件不以使用MainFrame布局的grid为中心。所以我决定将它们放在3个不同的帧上并将它们添加到MainFrame中。尽管如此,他们还是没有中心。如何让他们在我画的广场上?

from Tkinter import *


class MainFrame:
    def __init__(self, master):
        self.frame = Frame(master)#, width = 300, height = 250)
        self.frame.pack()

        self.createFrames()
        self.createCheckBoxes()
        self.createButtons()


    def createButtons(self):
        self.printButton = Button(self.frame0, text = "Print msg", command = self.printMsg)
        self.printButton.grid()

        self.printButton2 = Button(self.frame0, text = "Print msg 2", command = self.printMsg)
        self.printButton2.grid()

        self.quitButton = Button(self.frame2, text = "QUIT", command = self.frame.quit, fg = "red")
        self.quitButton.grid(columnspan = 10)


    def createCheckBoxes(self):
        self.cb1var = IntVar()
        self.cb1 = Checkbutton(self.frame1, text = "Checkbox 1", variable = self.cb1var, command = self.printMsgCb)
        self.cb1.grid()

    def createFrames(self):
        self.frame0 = Frame(self.frame)
        self.frame0.grid(row = 0, column = 0)
        self.frame0.grid_rowconfigure(0, weight=1)
        self.frame0.grid_columnconfigure(0, weight=1)
        self.frame1 = Frame(self.frame)
        self.frame1.grid(row = 0, column = 10)
        self.frame1.grid_rowconfigure(0, weight=1)
        self.frame1.grid_columnconfigure(0, weight=1)
        self.frame2 = Frame(self.frame)
        self.frame2.grid(row = 0, column = 20)
        self.frame2.grid_rowconfigure(0, weight=1)
        self.frame2.grid_columnconfigure(0, weight=1)

#button commands ----------------------------------
    def printMsg(self):
        print "Clicou no botao"
    def printMsgCb(self):
        print "Check box value = " + str(self.cb1var.get())
#--------------------------------------------------
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
frame = MainFrame(root)
root.geometry('1100x600+200+150')
root.mainloop()

my draw

1 个答案:

答案 0 :(得分:3)

首先,让我们失去一些对当前代码无能为力的冗余部分。的 删除

self.frame0.grid_rowconfigure(0, weight=1)
self.frame0.grid_columnconfigure(0, weight=1)
...   
self.frame1.grid_rowconfigure(0, weight=1)
self.frame1.grid_columnconfigure(0, weight=1)
...
self.frame2.grid_rowconfigure(0, weight=1)
self.frame2.grid_columnconfigure(0, weight=1)

以上行正在配置当前不需要配置的子帧(self.frame0self.frame1self.frame2)的内部布局。

然后替换:

self.frame1.grid(row = 0, column = 10)
...
self.frame2.grid(row = 0, column = 20)

使用:

self.frame1.grid(row = 0, column = 1)
...
self.frame2.grid(row = 0, column = 2)

因为你现在不需要那么大的网格。

然后替换:

self.frame.pack()

使用:

self.frame.pack(fill='x', expand=True)

self.frame水平展开时将self.frame带到垂直中心。

最后通过添加:

,通过配置他们所在的列,水平地均匀分布self.frame.grid_columnconfigure(0, weight=1, uniform=True) self.frame.grid_columnconfigure(1, weight=1, uniform=True) self.frame.grid_columnconfigure(2, weight=1, uniform=True) 的子帧。
self.frame

在定义from Tkinter import * class MainFrame: def __init__(self, master): self.frame = Frame(master)#, width = 300, height = 250) self.frame.grid_columnconfigure(0, weight=1, uniform=True) self.frame.grid_columnconfigure(1, weight=1, uniform=True) self.frame.grid_columnconfigure(2, weight=1, uniform=True) self.frame.pack(fill='x', expand=True) self.createFrames() self.createCheckBoxes() self.createButtons() def createButtons(self): self.printButton = Button(self.frame0, text = "Print msg", command = self.printMsg) self.printButton.grid() # add sticky='nsew' optionally self.printButton2 = Button(self.frame0, text = "Print msg 2", command = self.printMsg) self.printButton2.grid() self.quitButton = Button(self.frame2, text = "QUIT", command = self.frame.quit, fg = "red") self.quitButton.grid(columnspan = 10) def createCheckBoxes(self): self.cb1var = IntVar() self.cb1 = Checkbutton(self.frame1, text = "Checkbox 1", variable = self.cb1var, command = self.printMsgCb) self.cb1.grid() def createFrames(self): self.frame0 = Frame(self.frame) self.frame0.grid(row = 0, column = 0) self.frame1 = Frame(self.frame) self.frame1.grid(row = 0, column = 1) self.frame2 = Frame(self.frame) self.frame2.grid(row = 0, column = 2) #button commands ---------------------------------- def printMsg(self): print "Clicou no botao" def printMsgCb(self): print "Check box value = " + str(self.cb1var.get()) #-------------------------------------------------- root = Tk() root.grid_rowconfigure(0, weight=1) root.grid_columnconfigure(0, weight=1) frame = MainFrame(root) root.geometry('1100x600+200+150') root.mainloop() 之后的任何地方。

已完成所有更改的代码:

!important