Python 2.7 Tkinter小部件没有显示

时间:2014-04-18 20:17:31

标签: python tkinter

我正在设置一个Tkinter应用程序,出于某种原因,基本小部件没有显示。我得到一个空白的Tkinter窗口,没有别的。

以下是我的代码。我尝试添加简单的小部件,但这不起作用。

这是我的代码:

import Tkinter as Tk
import ttk as ttk

class MainApplication(Tk.Frame):
    def __init__(self, root):
        Tk.Frame.__init__(self)
        self.root = root
        self.root.title('JRSuite')
        root.attributes('-fullscreen', True)
        self.mainWindow = Tk.Frame(self)
        self.mainWindow.pack()
        self._windowSetup()

     def _windowSetup(self):
        '''Sets up the basic components of the main window'''
        self.tree = ttk.Treeview(self.mainWindow)
        self.tree.pack()
        self.note = ttk.Notebook(self.mainWindow)
        self.note.pack()
        self.tree.insert('', 'end', text = 'Woohoo')

if __name__ == '__main__':
root = Tk.Tk()
app = MainApplication(root)
app.mainloop()

2 个答案:

答案 0 :(得分:4)

你应该pack该应用:

if __name__ == '__main__':
    root = Tk.Tk()
    app = MainApplication(root)
    app.pack()
    app.mainloop()

答案 1 :(得分:0)

  

问题:Tkinter小部件未显示

不是从Tk.Frame继承,而是从Tk.Tk窗口root继承。
更改为:

import Tkinter as Tk
import ttk as ttk

class MainApplication(Tk.Tk):
    def __init__(self):
        Tk.Frame.__init__(self)
        self.title('JRSuite')
        self.attributes('-fullscreen', True)

        self.mainWindow = Tk.Frame(self)
        self.mainWindow.pack()
        self._windowSetup(self.mainWindow)

     def _windowSetup(self, parent):
        '''Sets up the basic components of the main window'''
        self.tree = ttk.Treeview(parent)
        self.tree.pack()
        self.note = ttk.Notebook(parent)
        self.note.pack()
        self.tree.insert('', 'end', text = 'Woohoo')

if __name__ == '__main__':
    MainApplication().mainloop()
相关问题