I need to import and run script from module1 only when I call function by pressing button in main1. But it does not work. Import works if I import module1 in main1 module in the top of the script for main1 module. But I want the import should happen from the function. Also I need the imported module1 should be unloaded after it's job is done and the program should return back to main1 module
Both modules (python files) are in the same folder with empty __init__.py
file.
Code for main module
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
# import module1 # This import code works, but I need it
# to work only when #import_but gets pressed
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
app = QApplication(sys.argv)
def window():
global widget6
widget6 = QWidget()
widget6.setGeometry(100, 150, 1300, 800)
widget6.setHidden(False)
global start_frame
start_frame = QFrame(widget6)
start_frame.setGeometry(5, 5, 1300, 800)
start_frame.setStyleSheet("QFrame {font-size: 13pt;}")
start_frame.setHidden(False)
global import_but
import_but = QPushButton(start_frame)
import_but.setGeometry(1050, 200, 200, 80)
import_but.setHidden(False)
import_but.setStyleSheet("QPushButton{font-size: 12pt;}")
import_but.setText("Import Modul1")
import_but.pressed.connect(import_but_pressed)
def import_but_pressed(): # This function does not work
import modul1
app.exec_()
# if __name__ == 'main1':
if __name__ == '__main__':
window()
sys.exit(app.exec_())
Code for module1
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QListView
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
app = QApplication(sys.argv)
def window():
global widget
widget = QWidget()
widget.setGeometry(100, 150, 1300, 800)
widget.setHidden(False)
w_list = QListView(widget)
w_list.setStyleSheet("QListView{font-size: 12pt;}")
w_list.setGeometry(30, 30, 250, 300)
w_list.setHidden(False)
m_list = QListView(widget)
m_list.setGeometry(30, 400, 250, 300)
m_list.setStyleSheet("QListView{font-size: 12pt;}")
m_list.setHidden(False)
button2 = QPushButton(widget)
button2.setText("Finish")
button2.setGeometry(1100, 600, 150, 150)
button2.setStyleSheet("QPushButton{font-size: 12pt;}")
button2.setHidden(False)
def button2_pressed():
widget.close()
# Here has to be placed code for returning to main1.py
button2.pressed.connect(button2_pressed)
app.exec_()
if __name__ == 'modul1':
# if __name__ == '__main__':
window()
sys.exit(app.exec_())
Any kind of help and support is welcome! Thank you!