python - 从自己的类调用 iPyWidgets 不显示

标签 python python-3.x ipywidgets

我尝试通过我自己的类从 iPyWidgets 调用按钮,但不幸的是它不会显示小部件。但是当我直接在控制台(Jupyterhub)中调用小部件代码时,它会显示该按钮。这是我的代码

callButton.py

from here.analysis.testUI import TestUI
def call_button():
    test = TestUI()
    go = test.button()
    go

testUi.py

from ipywidgets import widgets
from IPython.display import display
from ipywidgets import Button, HBox, VBox, Layout, Button, Text, Textarea

class TestUI():
    def __init__(self):
        print("Just initialized")

    def button(self):
        #Maybe a return is not needed, but it also doesn´t work without a return
        return widgets.Button(description = 'clear', layout=Layout(width='20%', height='100%'))

我在 Python 控制台中调用它,如下所示:

from here.analysis.callButton import call_button
call_button()

那么为什么按钮没有显示呢?我错过了什么吗?

最佳答案

确保使用 call_button.py 中的 display(button) 显式显示按钮

关于python - 从自己的类调用 iPyWidgets 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803555/

相关文章:

python - 在 Django 启动时初始化一个类,然后在 View 中引用它

python-3.x - 类实例上的 delattr 产生意外的 AttributeError

python - 使用交互改变另一个参数时重置参数

python - 如何在 Python 中屏蔽二维列表?

python - Tkinter 工作吗?

ipython - 如何在 ipywidgets 上定义 Slider 小部件的默认值?

Python - 动态选择新数据框的列

python - 将 pandas 数据框渲染为 html,突出显示特定元素

Python 惰性字典评估

python - matplotlib 中的 "Repel"注释?