简体   繁体   中英

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. I'm getting a blank Tkinter window, and nothing else.

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:

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

Question : Tkinter widgets not showing

Instead of inheriting from Tk.Frame inherit from Tk.Tk which is the root window.
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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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