search

Home  >  Q&A  >  body text

pyqt4 - python,pyqt,一个文件用另一个文件的全局变量,居然报'module' object has no attribute

这个是uitest.py文件的

if __name__ == "__main__":
    global ui
    import sys
    app = QtGui.QApplication(sys.argv)
    Dialog = QtGui.QDialog()
    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
    
这个是另一个py文件,我想调用上面那个py文件的ui变量,然后报错'module' object has no attribute ‘ui'

uitest.ui.tableWidget.setColumnCount(9)
高洛峰高洛峰2802 days ago869

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:12:08

    I don’t quite understand why the questioner did this, but if you want to get the variables of another file, I suggest you write a function, which is better.

    For example, if the variable ui you want to call is in the test1.py file, then you can write such a function in test1.py

    class Test():
        def __init__():
            ...
            
        def getUi():
            return ui

    Then in test2.py code example:

    test = Test()
    ui = test.getUi()

    That’s it

    reply
    0
  • 阿神

    阿神2017-04-18 10:12:08

    How did you import uitest?


    Change it to this~

    global ui
    if __name__ == "__main__":
        #…………

    reply
    0
  • Cancelreply