简体   繁体   English

python3中的Tkinter,菜单栏不起作用

[英]Tkinter in python3, menubar does not working

Well, i want to add menubar, but something is going wrong. 好吧,我想添加菜单栏,但是出了点问题。

It says: AttributeError: 'NoneType' object has no attribute 'config' 它说:AttributeError:'NoneType'对象没有属性'config'

My code: 我的代码:

from tkinter import *


class ApplicationWindow(Tk):

    def __init__(self, master=None):
        Tk.__init__(self, master)
        self.master = master
        self.geometry('800x400')
        self.f_app = Frame(self).pack()
        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = Menu(menubar)
        fileMenu.add_command(label="Exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=fileMenu)
        self.b_log = Button(self, width=10, text="Войти", command=self.func).pack()


    def onExit(self):
        self.quit()

    def func(self):
        print("hello")

def main():
    # root = tk
    app = ApplicationWindow() 
    app.mainloop()


if __name__ == '__main__':
    main()

You're initializing your ApplicationWindow class without passing any arguments in, like this app = ApplicationWindow() . 您将在不传递任何参数的情况下初始化ApplicationWindow类,例如此app = ApplicationWindow() In your init method, you give master a None default, and when you try to use master.config it says 在您的init方法中,您为master了默认值None ,当您尝试使用master.config它说

'NoneType' object has no attribute 'config' 'NoneType'对象没有属性'config'

Try passing an argument in when you initialize the instance of ApplicationWindow . 初始化ApplicationWindow实例时,尝试传递一个参数。 Whatever it is that you want master to be (just not a None object). 无论您希望master是什么(不是None对象)。

I have updated your code (below) and it runs. 我已经更新了您的代码(如下),它可以运行。 The button works, and the exit function closes the window. 该按钮起作用,并且退出功能关​​闭该窗口。 There was a lot to fix, but it runs without error. 有很多要修复的东西,但是它运行没有错误。 Take it from here: 从这里获取:

import tkinter


class ApplicationWindow(tkinter.Tk):

    def __init__(self, master=None):
        # Tk.__init__(self, master)
        self.master = master
        self.master.geometry('800x400')
        self.master.f_app = tkinter.Frame(self.master).pack()
        menubar = tkinter.Menu(self.master)
        self.master.config(menu=menubar)

        fileMenu = tkinter.Menu(menubar)
        fileMenu.add_command(label="Exit", command=self.onExit)
        menubar.add_cascade(label="File", menu=fileMenu)
        self.b_log = tkinter.Button(self.master, width=10, text="Войти", command=self.func).pack()


    def onExit(self):
        self.master.destroy()

    def func(self):
        print("hello")

def main():
    root = tkinter.Tk()
    app = ApplicationWindow(root) 
    root.mainloop()


if __name__ == '__main__':
    main()

You have an argument named master=None defaults to None. 您有一个名为master=None的参数,默认为None。 So when you create an instance of ApplicationWindow() without parameter your master argument gets None, and here you are calling config() method but your master is none and it doesnt have a method named config. 因此,当您创建不带参数的ApplicationWindow()实例时,您的master参数将变为None(无),在这里您将调用config()方法,但您的主参数为none,并且它没有名为config的方法。

class ApplicationWindow(Tk):
    def __init__(self, master=None):
        ...
        self.master.config(menu=menubar) # Error accurred here

def main():
    # root = tk
    app = ApplicationWindow() # pass an argument

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

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