[英]GUI with tkinter does not show up
I am new to tkinter and have big problem with the GUI. 我是tkinter的新手,并且GUI有很大的问题。 My idea is to create a Menubar here and I have it already implemented (below you can find main.py and menu.py). 我的想法是在这里创建一个菜单栏,并且已经实现了(在下面可以找到main.py和menu.py)。
Unfortunately, when I run the program only a very, very small window appears. 不幸的是,当我运行程序时,只会出现一个很小的窗口。 It's so small that I can't really see it. 它是如此之小,以至于我看不到它。 But I don't understand why. 但是我不明白为什么。 What did I do wrong here? 我在这里做错了什么?
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()
That's the menu.py: 那是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
I am not knowledgeable with the use of super()
. 我对使用super()
不了解。 I have replaced it with tk.Frame
and made a few other changes which is accompanied with explanation/comment. 我已将其替换为tk.Frame
并进行了一些其他更改,并附有说明/评论。 See the revised codes given below. 请参阅下面给出的修订代码。 For the revised menu.py, you have to append the relevant sections from where I left off for the code to work (save display space). 对于修改后的menu.py,您必须在我停下的地方附加相关部分,以使代码起作用(节省显示空间)。
I was able to get a non-zero width Tk window when the script was executed. 执行脚本时,我可以获得一个非零宽度的Tk窗口。 Hope this solution helps you answer your question. 希望此解决方案可以帮助您回答问题。
Revised Main.py 修改后的Main.py
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()
Revised menu.py (Relevant part) : 修改后的menu.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)
As I said in comments, apparently one can't attach a tk.Menu
to a tk.Frame
and have it things work correctly—however a workaround using tk.Menubutton
was suggested, so here's a modified version of your menu.py
that demonstrates how to implement said method of overcoming that limitation. 正如我在评论中说的,显然不能将tk.Menu
附加到tk.Frame
并使它正常工作-但是建议使用tk.Menubutton
一种解决方法,因此这是menu.py
的修改版,用于演示如何实现克服上述限制的方法。
Basically each top menu item is now a tk.Menubtton
. 现在基本上每个顶级菜单项都是tk.Menubtton
。 The choices (picks) underneath each one are still separate tk.Menu
s as before. 每个选项下面的选择(选项)仍然像以前一样是单独的tk.Menu
。 I also used the grid
layout manager to arrange them in a single row. 我还使用了grid
布局管理器将它们排列在一行中。 You can probably do the same thing with pack
, but as I mentioned, I'm not familiar with it. 您可以使用pack
进行相同的操作,但是正如我提到的,我对此并不熟悉。 Besides, there's no harm in an independent tk.Frame
using a different layout manager than its parent does. 此外,使用与父代不同的布局管理器在独立的tk.Frame
也没有害处。
No changes were made or required to your main.py
file. 您的main.py
文件未进行更改或不需要。 I put all those dummy callback functions you added to menu.py
into a separate callbacks.py
file which is now import
ed it near the beginning in the revised version of the file below. 我将所有添加到menu.py
虚拟回调函数放入一个单独的callbacks.py
文件中,该文件现在在以下文件的修订版的开头附近将其import
。
Modified menu.py
: 修改过的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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.