带有tkinter的GUI未显示

时间:2017-02-19 21:51:18

标签: python python-3.x user-interface tkinter

我是tkinter的新手,并且GUI存在很大问题。 我的想法是在这里创建一个菜单栏,我已经实现了它(下面你可以找到main.py和menu.py)。

不幸的是,当我运行程序时,只会出现一个非常非常小的窗口。它太小了,我无法真正看到它。但我不明白为什么。 我在这里做错了什么?

import tkinter as tk

from menu import Menu
#from toolbar import Toolbar
#from content import Content

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent

        self.menu = Menu(self, background="yellow")

        # Toolbar and Content not implemented yet
        # self.toolbar = Toolbar(self, height=25, background="green")
        # self.content = Content(self, background="red")

        self.menu.pack()
        #self.toolbar.pack()
        #self.content.pack()

if __name__ == "__main__":
    root = tk.Tk()
    root.title("Editor")
    app = MainApplication(root)
    app.pack()
    root.config(menu=app.menu.menubar)
    root.mainloop()

这是menu.py:

import tkinter as tk

class Menu(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)
        self.parent = parent
        self.menubar = tk.Menu(self)

        self.create_file_menu(self.menubar)
        self.create_edit_menu(self.menubar)
        self.create_view_menu(self.menubar)
        self.create_about_menu(self.menubar)

    def create_file_menu(self, parent):
        self.file_menu = tk.Menu(parent, tearoff=False)
        self.menubar.add_cascade(label="File", menu=self.file_menu)

        # Commands
        self.file_menu.add_command(label="New", accelerator="Ctrl+N", command=new_callback)
        self.file_menu.add_command(label="Open", accelerator="Ctrl+O", command=open_callback)
        self.file_menu.add_command(label="Save", accelerator="Ctrl+S", command=save_callback)
        self.file_menu.add_command(label="Save as", accelerator="Shift+Ctrl+S", command=saveas_callback)
        self.file_menu.add_separator()
        self.file_menu.add_command(label="Exit", accelerator="Alt+F4", command=exit_callback)

    def create_edit_menu(self, parent):
        self.edit_menu = tk.Menu(parent, tearoff=False)
        self.menubar.add_cascade(label="Edit", menu=self.edit_menu)

        # Commands
        self.edit_menu.add_command(label="Undo", accelerator="Ctrl+Z", command=undo_callback)
        self.edit_menu.add_command(label="Redo", accelerator="Ctrl+Y", command=redo_callback)
        self.edit_menu.add_separator()
        self.edit_menu.add_command(label="Cut", accelerator="Ctrl+X", command=cut_callback)
        self.edit_menu.add_command(label="Copy", accelerator="Ctrl+C", command=copy_callback)
        self.edit_menu.add_command(label="Paste", accelerator="Ctrl+V", command=paste_callback)
        self.edit_menu.add_separator()
        self.edit_menu.add_command(label="Find", accelerator="Ctrl+F", command=find_callback)
        self.edit_menu.add_separator()
        self.edit_menu.add_command(label="Select all", accelerator="Ctrl+A", command=selectall_callback)

    def create_view_menu(self, parent):
        self.view_menu = tk.Menu(parent, tearoff=False)
        self.menubar.add_cascade(label="View", menu=self.view_menu)

    def create_about_menu(self, parent):
        self.about_menu = tk.Menu(parent, tearoff=False)
        self.menubar.add_cascade(label="About", menu=self.about_menu)

        # Commands
        self.about_menu.add_command(label="About", command=about_callback)
        self.about_menu.add_command(label="Help", command=help_callback)

def new_callback():
    pass

def open_callback():
    pass

def save_callback():
    pass

def saveas_callback():
    pass

def exit_callback():
    pass

def undo_callback():
    pass

def redo_callback():
    pass

def cut_callback():
    pass

def copy_callback():
    pass

def paste_callback():
    pass

def find_callback():
    pass

def selectall_callback():
    pass

def about_callback():
    pass

def help_callback():
    pass

2 个答案:

答案 0 :(得分:0)

我对tk.Frame的使用知之甚少。我已将其替换为import tkinter as tk from menu import Menu class MainApplication(tk.Frame): def __init__(self, parent, *args, **kwargs): #super().__init__(parent, *args, **kwargs) #replaced with below sentence tk.Frame.__init__(self, parent, *args, **kwargs) #added self.parent = parent #self.menu = Menu(self, background="yellow") #replaced with below 3 sentences self.menu = Menu(self) #added: Instantiate the class Menu as self.menu self.menubar = self.menu.menubar #added: relate the local variable for menubar(i.e. self.menubar) with the menubar variable in the instance self.menu. self.menubar.configure(background = 'yellow') #added, configure the background # If you comment the above 3 sentences and uncomment the below, # you will get the same window """self.menubar = tk.Menu(self.parent, background="yellow") self.create_file_menu(self.menubar) self.create_edit_menu(self.menubar) self.create_view_menu(self.menubar) self.create_about_menu(self.menubar) def create_file_menu(self, parent): self.file_menu = tk.Menu(parent, tearoff=False) self.menubar.add_cascade(label="File", menu=self.file_menu) def create_edit_menu(self, parent): self.edit_menu = tk.Menu(parent, tearoff=False) #self.menubar.add_cascade(label="Edit", menu=self.edit_menu) def create_view_menu(self, parent): self.view_menu = tk.Menu(parent, tearoff=False) self.menubar.add_cascade(label="View", menu=self.view_menu) def create_about_menu(self, parent): self.about_menu = tk.Menu(parent, tearoff=False) self.menubar.add_cascade(label="About")""" if __name__ == "__main__": root = tk.Tk() root.title("Editor") app = MainApplication(root) #app.pack() # this caused the zero width Window root.configure(menu=app.menu.menubar) root.mainloop() 并进行了一些其他更改,并附有解释/评论。请参阅下面给出的修订代码。对于修改后的menu.py,您必须附加我离开的相关部分才能使代码生效(保存显示空间)。

执行脚本时,我能够得到一个非零宽度的Tk窗口。希望这个解决方案可以帮助您回答问题。

修订Main.py

import tkinter as tk

class Menu(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        #super().__init__(parent, *args, **kwargs) #replaced with below sentence
        tk.Frame.__init__(self, parent, *args, **kwargs) #added
        self.parent = parent
        self.menubar = tk.Menu(self)

修改了menu.py(相关部分)

prepareFunction

答案 1 :(得分:0)

正如我在评论中所说的那样,显然有人无法将tk.Menu附加到tk.Frame并让它正常工作 - 但建议使用tk.Menubutton进行解决方法,所以这里是menu.py的修改版本,演示了如何实现克服该限制的所述方法。

基本上每个顶级菜单项现在都是tk.Menubtton。每个选项下面的选项(选秀权)仍然像以前一样独立tk.Menu。我还使用grid布局管理器将它们排列在一行中。你可以用pack做同样的事情,但正如我所提到的,我并不熟悉它。此外,使用与其父级不同的布局管理器的独立tk.Frame没有任何害处。

您的main.py文件未进行任何更改。我将您添加到menu.py的所有虚拟回调函数放入一个单独的callbacks.py文件中,该文件现在import位于下面文件的修订版本的开头附近。

修改 menu.py

import tkinter as tk
from callbacks import *  # all the dummy callback functions...

class Menu(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.create_file_menu()
        self.create_edit_menu()
        self.create_view_menu()
        self.create_about_menu()

    def create_file_menu(self):
        self.file_menu = tk.Menubutton(self, text='File')
        picks = tk.Menu(self.file_menu)
        self.file_menu.config(menu=picks)

        # Commands
        picks.add_command(label='New', accelerator='Ctrl+N', command=new_callback)
        picks.add_command(label='Open', accelerator='Ctrl+O', command=open_callback)
        picks.add_command(label='Save', accelerator='Ctrl+S', command=save_callback)
        picks.add_command(label='Save as', accelerator='Shift+Ctrl+S', command=saveas_callback)
        picks.add_separator()
        picks.add_command(label='Exit', accelerator='Alt+F4', command=exit_callback)

        self.file_menu.grid(row=0, column=0)

    def create_edit_menu(self):
        self.edit_menu = tk.Menubutton(self, text='Edit')
        picks = tk.Menu(self.edit_menu)
        self.edit_menu.config(menu=picks)

        # Commands
        picks.add_command(label='Undo', accelerator='Ctrl+Z', command=undo_callback)
        picks.add_command(label='Redo', accelerator='Ctrl+Y', command=redo_callback)
        picks.add_separator()
        picks.add_command(label='Cut', accelerator='Ctrl+X', command=cut_callback)
        picks.add_command(label='Copy', accelerator='Ctrl+C', command=copy_callback)
        picks.add_command(label='Paste', accelerator='Ctrl+V', command=paste_callback)
        picks.add_separator()
        picks.add_command(label='Find', accelerator='Ctrl+F', command=find_callback)
        picks.add_separator()
        picks.add_command(label='Select all', accelerator='Ctrl+A', command=selectall_callback)

        self.edit_menu.grid(row=0, column=1)


    def create_view_menu(self):
        self.view_menu = tk.Menubutton(self, text='View')
        picks = tk.Menu(self.view_menu)
        self.view_menu.config(menu=picks)

        # Commands
        submenu = tk.Menu(picks)
        picks.add_cascade(label='Views', menu=submenu)
        submenu.add_command(label='View 1', command=lambda: None)
        submenu.add_command(label='View 2', command=lambda: None)

        self.view_menu.grid(row=0, column=2)

    def create_about_menu(self):
        self.about_menu = tk.Menubutton(self, text='About')
        picks = tk.Menu(self.about_menu)
        self.about_menu.config(menu=picks)

        # Commands
        picks.add_command(label='About', command=about_callback)
        picks.add_command(label='Help', command=help_callback)

        self.about_menu.grid(row=0, column=3)