2


我想用 Tkinter 创建一个 GUI 程序。我的想法是一个带有 menuBar 和 MainFrame 的 mainWindow,它显示动态内容。
我想通过以下方式添加 MainFrame:

MainFrame = Frame(self, bd = '2')
MainFrame.pack(anchor = CENTER)

但什么也没发生。有什么想法吗?谢谢你的帮助。



程序代码:

class View(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.title('Device Configurator')
        self.geometry('500x400')
        self.resizable(0,0)

        self.countFrame = Frame(self, bd = 2, relief = RIDGE)
        self.countFrame.pack(pady = 10, padx = 5)    

        MainFrame = Frame(self, bd = '2')
        MainFrame.pack(anchor = CENTER)

        menubar = Menu(self)
        filemenu = Menu(menubar, tearoff=0)
        filemenu.add_command(label='Configure Devices', command= None)
        filemenu.add_command(label='Exit', command=self.quit)
        menubar.add_cascade(label='File', menu=filemenu)

        infomenu = Menu(menubar, tearoff = 0)
        infomenu.add_command(label='About', command = None)
        menubar.add_cascade(label='Info', menu = infomenu)

        self.config(menu = menubar)



class Controller(object):
    def __init__(self):
        self.view = View()
        self.view.mainloop()

c = Controller()
4

1 回答 1

3

实际上,您的框架已成功添加,但大小为零(而且不可见!)。

您可以添加一些内容,或者要求布局管理器为其提供所有可用空间MainFrame.pack(expand=True, fill="both")

为了使其可见,borderwith(alias bd) 是不够的。它必须与救济相结合(例如relief=SUNKEN)。“查看”框架的另一种方法是设置一个background.

于 2012-10-11T07:47:37.650 回答