简体   繁体   中英

Python script stops running when I import a GUI python script

I have a PyQt5 script that creates a window containing a slider. I would like the value of this slider to be used in the main python script, however when it is run the GUI script is continuously run and the main python script is not run at all.

I have created a simple slider in the GUI python script and then imported this script into the main python file.

my GUI python file is:

import sys
from PyQt5.QtWidgets import (QLineEdit, QSlider, QPushButton, 
QVBoxLayout, QApplication, QWidget)
from PyQt5.QtCore import Qt

class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.init_ui()

    def init_ui(self):
        self.s1 = QSlider(Qt.Horizontal)
        self.s1.setMinimum(1)
        self.s1.setMaximum(100)
        self.s1.setValue(25)
        self.b1 = QPushButton('Run')

        v_box = QVBoxLayout()
        v_box.addWidget(self.s1)
        v_box.addWidget(self.b1)

        self.setLayout(v_box)

        self.b1.clicked.connect(self.run_script)

        self.show()

    def run_script(self):
        return self.s1.value()

app = QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())

I would then like to take the value from the slider and use in my main python script, a simplified version would be:

from gui_script.py import * 

value = 10 + self.s1.value()
print(value)

From this I get the slider value however the 10 is not added to the slider value, meaning the main python file is not run. How do I get the main python file to run correctly when I have inputed the slider value?

I would expect the value to be 20 if say the slider value was placed at 10.

I would venture to guess that your GUI script is calling a .mainloop() or some equivalent and entering into a blocking loop. This is just speculation though without you posting any of the relevant code... Assuming this IS the issue you can perhaps circumvent it via threading or multiprocessing.

Try it:

main.py

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore    import *

# from gui_script.py import *                     # ---
from gui_script import Slider                     # +++

class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.slider = Slider()                    # +++
        self.slider.s1.valueChanged[int].connect(self.changeValue)

        self.spinBox = QSpinBox(self)
        self.spinBox.setMinimum(1)
        self.spinBox.setMaximum(100)
        self.spinBox.setValue(25)
        self.spinBox.valueChanged.connect(self.valueChangedSpin)

        lay = QVBoxLayout(self)
        lay.addWidget(self.slider)
        lay.addWidget(self.spinBox)

    def valueChangedSpin(self, i):
        self.slider.s1.setValue(i)

    def changeValue(self, value):                               
        self.spinBox.setValue(value)


if __name__ == '__main__':                        
    app = QApplication(sys.argv)
    mainwindow = Window()
    mainwindow.show()
    sys.exit(app.exec_())

gui_script.py

import sys
from PyQt5.QtWidgets import (QLineEdit, QSlider, QPushButton, 
                             QVBoxLayout, QApplication, QWidget, QLabel)
from PyQt5.QtCore    import Qt

class Slider(QWidget):
    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.label = QLabel("25")

        self.s1 = QSlider(Qt.Horizontal)
        self.s1.setMinimum(1)
        self.s1.setMaximum(100)
        self.s1.setValue(25)
        self.s1.valueChanged[int].connect(self.changeValue)    

        self.b1 = QPushButton('Run')
        self.b1.clicked.connect(self.run_script)

        self.v_box = QVBoxLayout()
        self.v_box.addWidget(self.label)
        self.v_box.addWidget(self.s1)
        self.v_box.addWidget(self.b1)
        self.setLayout(self.v_box)

    def run_script(self):
#        return self.s1.value()
        print(self.s1.value())

    def changeValue(self, value):                               
        self.label.setNum(value)  

if __name__ == '__main__':                      # !!!
    app = QApplication(sys.argv)
    a_window = Slider()
    a_window.show()
    sys.exit(app.exec_())

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM