简体   繁体   English

Python 2.7 Tkinter小部件未显示

[英]Python 2.7 Tkinter widgets not showing

I'm working on setting up a Tkinter app, for some reason, the basic widgets aren't showing. 我正在设置Tkinter应用程序,由于某种原因,基本的小部件没有显示。 I'm getting a blank Tkinter window, and nothing else. 我得到一个空白的Tkinter窗口,仅此而已。

The following is my code. 以下是我的代码。 I've tried adding simple widgets, and that's not working. 我尝试添加简单的小部件,但这没有用。

Here's the code I have: 这是我的代码:

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()

You ought to pack the app: 您应该pack该应用程序:

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

Question : Tkinter widgets not showing 问题 :Tkinter小部件未显示

Instead of inheriting from Tk.Frame inherit from Tk.Tk which is the root window. 而不是从Tk.Frame继承,而从Tk.Tk它是root窗口)继承。
Change to: 改成:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM