Trouble calling py script from another script [duplicate]

社会主义新天地 提交于 2019-12-11 17:15:31

问题


Im new to python, I created a py file from a ui file, the problem with that if i change something from the ui file nothing changes in py file therefore i made another py file that loads the ui file instead. With that if i change something in the ui file it also updates the py file. It is something like this.....

from PyQt5 import QtCore, QtGui, QtWidgets, uic

class Ui_DTR2(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_DTR2,self).__init__()
        uic.loadUi('dtr.ui',self)

if __name__=='__main__':
    import sys
    app=QtWidgets.QApplication(sys.argv)
    window=Ui_DTR2()
    window.show()
    sys.exit(app.exec_())

Now my problem is that how can i call the above py script from another py script?


回答1:


By reference you have some very well structured answers here which itself is a duplicate of this, which might make yours a duplicate also :)

In the .py script file simply import this one:

#!/usr/bin/python
import youpreviousfile    #without .py

And the other example:

test1.py

def some_func():
    print 'in test 1, unproductive'

if __name__ == '__main__':
    # test1.py executed as script
    # do something
    some_func()

service.py

import test1

def service_func():
    print 'service func'

if __name__ == '__main__':
    # service.py executed as script
    # do something
    service_func()
    test1.some_func()


来源:https://stackoverflow.com/questions/53407491/trouble-calling-py-script-from-another-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!