Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法访问UI文件中的QWidget,PySide_Python_Python 2.7_Pyside - Fatal编程技术网

Python 无法访问UI文件中的QWidget,PySide

Python 无法访问UI文件中的QWidget,PySide,python,python-2.7,pyside,Python,Python 2.7,Pyside,我在运行以下代码时遇到此错误 test.py AttributeError: 'PySide.QtGui.QWidget' object has no attribute 'test_button' test.ui文件有一个名为test_button的按钮,您不能以这种方式构造ui文件的路径: from PySide import QtGui, QtCore, QtUiTools module_dir = os.path.dirname('__file__') ui_file_path = m

我在运行以下代码时遇到此错误 test.py

AttributeError: 'PySide.QtGui.QWidget' object has no attribute 'test_button'

test.ui文件有一个名为test_button的按钮,您不能以这种方式构造ui文件的路径:

from PySide import QtGui, QtCore, QtUiTools
module_dir = os.path.dirname('__file__')
ui_file_path = module_dir + '/test.ui'
def call_ui():
    loader = QtUiTools.QUiLoader()
    ui_file = QtCore.QFile(ui_file_path)
    ui_file.open(QtCore.QFile.ReadOnly)
    ui = loader.load(ui_file)
    ui_file.close()
    return ui
class TestUI(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        self.ui = call_ui()
        self.setCentralWidget(self.ui)
        self.ui.test_button.clicked.connected(self.test)
    def test(self):
        print "Testing....."
def main():
    app = QtGui.QApplication(sys.argv)
    win = TestUI()
    win.show()
main()
将始终返回空字符串,并且ui文件路径将为“/test.ui”

正确的方法:

os.path.dirname('__file__')

但是当QUiLoader.load失败时,它会引发RuntimeError(在PySide 1.2.1上测试)。您的根文件系统上有ui文件吗?

谢谢您的回复,但我认为当我删除“self.ui.test\u按钮。clicked.connected(self.test)”时,这不是问题所在。它工作正常。当我试图从我的代码连接或修改任何小部件时,会弹出此错误。由于缺乏完整的回溯和您实际运行的代码,我无法重现您的问题(有太多的拼写错误)
module_dir = os.path.dirname(__file__)
ui_file_path = os.path.join(module_dir, 'test.ui')