Python Tkinter菜单栏不显示 [英] Python Tkinter menu bars don't display

查看:1161
本文介绍了Python Tkinter菜单栏不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Tkinter制作GUI,并开始实现菜单栏.我看过一些教程,并为此编写了一些代码,但是菜单栏似乎从未出现过-只是一个带有白色背景的空白框.但是,这不仅发生在我的代码中.在将上述教程之一的代码复制并粘贴到新脚本中时,会表现出相同的行为.

I'm trying to make a GUI using Tkinter and have come to implementing a menu bar. I've looked at a few tutorials and written some code for it, but a menu bar never seems to appear - just a blank frame with a white background. This doesn't just happen for my code though; on copying and pasting the code of one of the aforementioned tutorials into a new script, the same behaviour is exhibited.

如果有人能弄清是什么原因,我将不胜感激.我的系统是OS X 10.5,Python 2.7,Tk 8.4.这是本教程中似乎无效的代码:

I'd appreciate it if anyone could shed any light on what's causing this. My system is OS X 10.5, Python 2.7, Tk 8.4. Here's the code from the tutorial that doesn't appear to work:

#!/usr/local/bin/python2.7

from Tkinter import *
from ttk import *

class App(Frame):
    def __init__(self):
            Frame.__init__(self)

            self.master.geometry('400x300')
            self.master.title(__file__)

            self.pack()

            self.menu = Menu(tearoff=False)
            self.master.config(menu = self.menu)

            fm = self.file_menu = None
            fm = Menu(self.menu, tearoff=False)
            self.menu.add_cascade(label='File', menu = fm)

            fm.add_command(label='Say Hello', command = self.say_hello)
            fm.add_separator()
            fm.add_command(label='Quit', command = self.quit)

            self.mainloop()

    def say_hello(self, *e):
            self.label = Label(self.master, text='Hello there!')
            self.label.pack(anchor=CENTER, fill=NONE, expand=YES, side=LEFT)

if __name__ == '__main__':
    App()

我的代码在这里:

from Tkinter import *

class App(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent)

        parent.title("Cluedo Solver 1.0")

        menubar = Menu(root)
        menubar.add_command(label="File")
        menubar.add_command(label="Quit", command=root.quit())

        root.config(menu=menubar)

root=Tk()
root.geometry("300x250+300+300")
app=App(root)
root.mainloop()

推荐答案

带说明的代码

从个人经验来看,我发现通常使用widgets方法管理所有widget更加容易.那就是我在这里所做的,并且奏效了.另外,我不是父母,而是主人.我现在将逐步引导您完成代码.

From personal experience, I have found that it is usually easier to manage all widgets in a widgets method. That is what I did here, and it worked. Also, instead of parent, I used master. I will now walk you through the code step-by-step.

from Tkinter import *

我们导入Tkinter(GUI内容)

We import Tkinter (GUI stuff)

class App(Frame):

我们创建一个名为App的类,该类是存放小部件的Frame.

We create a class called App, which is the Frame where widgets are held.

    def __init__(self, master):
        Frame.__init__(self, master)
        self.grid()
        self.widgets()

我们创建一个名为__init__的方法.这将初始化该类,并运行另一个名为widgets的方法.

We create a method called __init__. This initializes the class, and runs another method called widgets.

    def widgets(self):


        menubar = Menu(root)
        menubar.add_command(label="File")
        menubar.add_command(label="Quit", command=root.quit())

        root.config(menu=menubar)

我们创建widgets方法.这是添加小部件menubar的位置.如果我们要创建更多的小部件,它们也将在这里.

We create the widgets method. This is where the widget, menubar is added. If we were to create anymore widgets, they would also be here.

root=Tk()
root.title("Menubar")
app=App(root)
root.mainloop()

最后,我们为整个窗口提供一些属性.我们给它命名为Menubar,然后运行App类.最后,我们用root.mainloop启动GUI的主循环.

Lastly, we give the entire window some properties. We give it a title, Menubar, and run the App class. lastly, we start the GUI's mainloop with root.mainloop.

这篇关于Python Tkinter菜单栏不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆