【问题标题】:AttributeError: bind missingAttributeError:绑定丢失
【发布时间】:2018-06-12 12:17:44
【问题描述】:

我正在尝试使用 vpython 模块(由于某些原因而不是 tkinter)创建一个按钮,但出现此错误:AttributeError: bind missing

什么是绑定,我该如何解决?

from vpython import *
def change(): # Appelé par controls quand on clique sur le bouton
    if b.text == 'Click me':
        b.text = 'Try again'
    else:
        b.text = 'Click me'

c = controls() # Crée une fenêtre pour les contrôles
# Crée un bouton dans la fenêtre des contrôles:
b = button( pos=(0,0), width=60, height=60, 
          text='Click me', action=lambda: change() )
while 1:
    c.interact()

【问题讨论】:

  • 哪一行给出了错误?此外,发布的空白看起来不正确。这正是导致问题的代码吗?
  • 好像少了from visual.controls import *,正如官方指南所说:vpython.org/contents/docs/controls.html
  • 提示:始终用英文记录您的代码。假设您编写了一些非常有用的代码并分享它,想象一下其他不使用该语言的开发人员会遇到的困难。
  • @Fusselldieb :对不起,你是对的。下次我会努力的
  • @Torxed :我之前尝试过这段代码,但出现了这个错误:ModuleNotFoundError: No module named 'visual' 我在stackoverflow上寻找它,我找到了这个主题:stackoverflow.com/questions/28592211/…说你必须替换由 vpython 可视化,但 vpython 没有名为 controls 的模块...

标签: python bind attributeerror vpython


【解决方案1】:

从下面的例子

https://github.com/BruceSherwood/vpython-jupyter/blob/master/Demos/ButtonsSlidersMenus2.ipynb

有行

cbutton = button(text='Red', textcolor=color.red, background=color.cyan, pos=scene.title_anchor, bind=Color)

所以看起来你需要为你的按钮添加绑定属性。

b = 按钮( pos=(0,0), width=60, height=60, text='点击我', action=lambda: change(), bind = change )

来自有关 VPython 7 中的按钮和小部件的文档。

http://www.glowscript.org/docs/VPythonDocs/controls.html

它说按钮的绑定属性是

"bind 点击按钮时要调用的函数名。创建按钮后不能更改。"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2011-09-25
    相关资源
    最近更新 更多