如何将类内部的函数作为菜单按钮的命令引用?

huangapple go评论107阅读模式
英文:

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

问题

这是代码:

  1. class applicationUI(Frame):
  2. def vGuitarRender():
  3. print("渲染中")
  4. def __init__(self, master):
  5. Frame.__init__(self, master)
  6. self.master = master
  7. menu = Menu(master)
  8. master.config(menu=menu)
  9. menu.add_command(label="虚拟吉他", command=vGuitarRender)

错误是这样的:

  1. menu.add_command(label="虚拟吉他", command=vGuitarRender)
  2. NameError: name 'vGuitarRender' is not defined

希望对此有一些建议。

英文:

This is the code:

  1. class applicationUI(Frame):
  2. def vGuitarRender():
  3. print("Rendering")
  4. def __init__(self, master):
  5. Frame.__init__(self, master)
  6. self.master = master
  7. menu = Menu(master)
  8. master.config(menu=menu)
  9. menu.add_command(label = "Virtual Guitar", command = vGuitarRender)

Error is this:

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

It would be great to have some advice on this.

答案1

得分: 2

vGuitarRender是您类中的一个方法,因此您需要使用self.vGuitarRender而不是。

  1. menu.add_command(label="虚拟吉他", command=self.vGuitarRender)
英文:

vGuitarRender is a method within your class, so you need to use self.vGuitarRender instead.

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

huangapple
  • 本文由 发表于 2020年1月3日 19:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定