如何以编程方式更新Traitsui的价值?

时间:2018-08-29 19:25:32

标签: python pyqt range enthought traitsui

我正在尝试更新Traitsui变量的值,以匹配通过PyQt接口给定的用户定义的变量。

这似乎很简单,但现在却花了我很多小时。我在这里使用的打印功能表明traits变量正在更新,但是实际的traits窗口没有刷新以匹配。

import sys, os
from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
from traits.api import HasTraits,Instance,on_trait_change,Int
from traitsui.api import View,Item,VGroup


class Traits_Scene(HasTraits):
    traitsui_value = 0

    def update_traitsui_value(self):
        Traits_Scene.traitsui_value = P1.pyqt_value
        print 'Traitsui value =',self.traitsui_value

    view = View(
                VGroup(
                    Item('traitsui_value',springy=False,show_label=True,style='readonly'),
                    ),
                resizable=True,
                )

class P1(QtGui.QWidget):
    pyqt_value = 0
    print 'PyQt value =',pyqt_value

    def __init__(self, parent=None):
        super(P1, self).__init__(parent)
        layout = QtGui.QGridLayout(self)

        def set_value():
            P1.pyqt_value = self.entry.value()
            print 'PyQt value =',P1.pyqt_value

            Traits_Scene().update_traitsui_value()

        self.entry = QtGui.QSpinBox(self)
        self.entry.setMinimum(0)
        self.entry.setMaximum(10)
        self.entry.setValue(0)
        self.entry.setPrefix('PyQt value: ')
        self.entry.valueChanged.connect(set_value)
        layout.addWidget(self.entry, 0, 0, 1, 1)
        self.entry.show()

        self.viz1 = Traits_Scene()
        self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
        layout.addWidget(self.ui1, 1, 0, 1, 1)

class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.window = P1(self) 
        self.setCentralWidget(self.window)
        self.show()

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    w = MainWindow()
    sys.exit(app.exec_())

0 个答案:

没有答案