简体   繁体   中英

How do I reference a function that is within the class as a command for a menu button?

This is the code:

class applicationUI(Frame):

    def vGuitarRender():
        print("Rendering")

    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        menu = Menu(master)
        master.config(menu=menu) 
        menu.add_command(label = "Virtual Guitar", command = vGuitarRender) 

Error is this:

menu.add_command(label = "Virtual Guitar", command = vGuitarRender)
NameError: name 'vGuitarRender' is not defined

It would be great to have some advice on this.

vGuitarRender是类中的一个方法,因此您需要改用self.vGuitarRender

menu.add_command(label = "Virtual Guitar", command = self.vGuitarRender) 

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