【问题标题】:Window not opening inPyQT5 GUI窗口未在 PyQT5 GUI 中打开
【发布时间】:2021-10-10 08:50:06
【问题描述】:

我正在尝试使用 Pyqt5 创建一个 GUI 应用程序。我面临的问题是代码运行良好,但 record 窗口没有打开。我附上了下面的整个代码和我所指的函数的 sn-p。我已经尝试过整个代码,但没有发现任何缺陷。

import sys
from random import randint

from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget
import time
#from libsonyapi.camera import Camera
#from libsonyapi.actions import Actions
#from pysony import SonyAPI, ControlPoint, common_header
#search = ControlPoint()
#cameras =  search.discover(1) 
#camera = SonyAPI(QX_ADDR=cameras[0])
 # create camera instance
#camerass = Camera()

###############################SECONDARY WINDOWS###############################################################################
        
class recording(QWidget):


    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        
        # THIS BUTTON IS USED TO START RECORDING #
        start_recording_button = QPushButton(" START RECORDING ")
        start_recording_button.clicked.connect(self.start_recording)
        layout.addWidget(start_recording_button)

        # THIS BUTTON IS USED TO STOP RECORDING #
        stop_recording_button = QPushButton(" STOP RECORDING ")
        stop_recording_button.clicked.connect(self.stop_recording)
        layout.addWidget(stop_recording_button)
        
        self.show()
        
    def start_recording(self):
        camerass.do(Actions.startMovieRec)
        time.sleep(2)

    def stop_recording(self):
        camerass.do(Actions.stopMovieRec)
        time.sleep(2)
        
        
class zoom(QWidget):


    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        
        # THIS BUTTON IS USED AS ZOOM IN #
        zoom_in_button = QPushButton(" ZOOM IN ")
        zoom_in_button.clicked.connect(self.zoom_in)
        layout.addWidget(zoom_in_button)

        # THIS BUTTON IS USED AS ZOOM OUT #
        zoom_out_button = QPushButton(" ZOOM OUT ")
        zoom_out_button.clicked.connect(self.zoom_out)
        layout.addWidget(zoom_out_button)

        # THIS BUTTON IS USED TO STOP THE ZOOM IN #
        stop_zoom_in_button = QPushButton(" STOP ZOOM IN ")
        stop_zoom_in_button.clicked.connect(self.stop_zoom_in)
        layout.addWidget(stop_zoom_in_button)
        
        # THIS BUTTON IS USED TO STOP THE ZOOM OUT #
        stop_zoom_out_button = QPushButton(" STOP ZOOM OUT ")
        stop_zoom_out_button.clicked.connect(self.stop_zoom_out)
        layout.addWidget(stop_zoom_out_button)
        
        self.show()
        
    def zoom_in(self):
        camera.actZoom(param=["in", "start"])
        time.sleep(2)
    
    def zoom_out(self):
        camera.actZoom(param=["out", "start"])
        time.sleep(2)
    
    def stop_zoom_in(self):
        camera.actZoom(param=["in", "stop"])
        time.sleep(2)

    def stop_zoom_out(self):
        camera.actZoom(param=["out", "stop"])
        time.sleep(2)
################################MAIN WINDOW 1##################################################################################
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.windows = []

        l = QVBoxLayout()
        button1 = QPushButton("CAMERA CONTROLS")
        button1.clicked.connect(self.open_newWindow)
        l.addWidget(button1)

        w = QWidget()
        w.setLayout(l)
        self.setCentralWidget(w)

    def open_newWindow(self):
        window = MainWindow2()
        self.windows.append(window)
        window.show()

###########################MAIN WINDOW 3########################################################################################
class MainWindow3(QMainWindow):
    def __init__(self):
        super().__init__()
        self.windows = []

        l = QVBoxLayout()
        zoom_controls_button = QPushButton("RECORDING")
        zoom_controls_button.clicked.connect(self.open_newWindow3)
        l.addWidget(zoom_controls_button)

        s = QWidget()
        s.setLayout(l)
        self.setCentralWidget(s)

    def open_newWindow3(self):
        window = recording()
        self.windows.append(window)
        window.show()
        

################################MAIN WINDOW 2##################################################################################

class MainWindow2(QMainWindow):
    def __init__(self):
        super().__init__()
        self.windows = []

        l = QVBoxLayout()
        zoom_controls_button = QPushButton("ZOOM CONTROLS")
        zoom_controls_button.clicked.connect(self.open_newWindow2)
        l.addWidget(zoom_controls_button)

        s = QWidget()
        s.setLayout(l)
        self.setCentralWidget(s)

    def open_newWindow2(self):
        window = zoom()
        self.windows.append(window)
        window.show()



app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()

而功能是

class recording(QWidget):


    def __init__(self):
        super().__init__()
        layout = QVBoxLayout(self)
        
        # THIS BUTTON IS USED TO START RECORDING #
        start_recording_button = QPushButton(" START RECORDING ")
        start_recording_button.clicked.connect(self.start_recording)
        layout.addWidget(start_recording_button)

        # THIS BUTTON IS USED TO STOP RECORDING #
        stop_recording_button = QPushButton(" STOP RECORDING ")
        stop_recording_button.clicked.connect(self.stop_recording)
        layout.addWidget(stop_recording_button)
        
        self.show()
        
    def start_recording(self):
        camerass.do(Actions.startMovieRec)
        time.sleep(2)

    def stop_recording(self):
        camerass.do(Actions.stopMovieRec)
        time.sleep(2)

【问题讨论】:

  • 你为什么使用time.sleep?请注意,在 GUI 元素中使用这样的阻塞函数被认为是一种可怕的做法。

标签: python pyqt5 qvboxlayout


【解决方案1】:

在您的代码中,您永远不会创建 MainWindow3,在单击“RECORDING”按钮后将创建记录类/窗口。

创建 MainWindow3 后,按下按钮后会显示录制窗口。 要对此进行测试,您只需替换

w = MainWindow()

w = MainWindow3()

在你的“主要”(倒数第三行)

【讨论】:

    【解决方案2】:

    我已在我的系统中运行它并进行了一些更改。我正在粘贴下面的代码。

    from random import randint
    
    from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QVBoxLayout, QWidget
    import time
    #from libsonyapi.camera import Camera
    #from libsonyapi.actions import Actions
    #from pysony import SonyAPI, ControlPoint, common_header
    #search = ControlPoint()
    #cameras =  search.discover(1) 
    #camera = SonyAPI(QX_ADDR=cameras[0])
     # create camera instance
    #camerass = Camera()
    
    ###############################SECONDARY WINDOWS###############################################################################
            
    class recording(QWidget):
    
    
        def __init__(self):
            super().__init__()
            layout = QVBoxLayout(self)
            
            # THIS BUTTON IS USED TO START RECORDING #
            start_recording_button = QPushButton(" START RECORDING ")
            start_recording_button.clicked.connect(self.start_recording)
            layout.addWidget(start_recording_button)
    
            # THIS BUTTON IS USED TO STOP RECORDING #
            stop_recording_button = QPushButton(" STOP RECORDING ")
            stop_recording_button.clicked.connect(self.stop_recording)
            layout.addWidget(stop_recording_button)
            
            self.show()
            
        def start_recording(self):
            camerass.do(Actions.startMovieRec)
            time.sleep(2)
    
        def stop_recording(self):
            camerass.do(Actions.stopMovieRec)
            time.sleep(2)
            
            
    class zoom(QWidget):
    
    
        def __init__(self):
            super().__init__()
            layout = QVBoxLayout(self)
            
            # THIS BUTTON IS USED AS ZOOM IN #
            zoom_in_button = QPushButton(" ZOOM IN ")
            zoom_in_button.clicked.connect(self.zoom_in)
            layout.addWidget(zoom_in_button)
    
            # THIS BUTTON IS USED AS ZOOM OUT #
            zoom_out_button = QPushButton(" ZOOM OUT ")
            zoom_out_button.clicked.connect(self.zoom_out)
            layout.addWidget(zoom_out_button)
    
            # THIS BUTTON IS USED TO STOP THE ZOOM IN #
            stop_zoom_in_button = QPushButton(" STOP ZOOM IN ")
            stop_zoom_in_button.clicked.connect(self.stop_zoom_in)
            layout.addWidget(stop_zoom_in_button)
            
            # THIS BUTTON IS USED TO STOP THE ZOOM OUT #
            stop_zoom_out_button = QPushButton(" STOP ZOOM OUT ")
            stop_zoom_out_button.clicked.connect(self.stop_zoom_out)
            layout.addWidget(stop_zoom_out_button)
            
            self.show()
            
        def zoom_in(self):
            camera.actZoom(param=["in", "start"])
            time.sleep(2)
        
        def zoom_out(self):
            camera.actZoom(param=["out", "start"])
            time.sleep(2)
        
        def stop_zoom_in(self):
            camera.actZoom(param=["in", "stop"])
            time.sleep(2)
    
        def stop_zoom_out(self):
            camera.actZoom(param=["out", "stop"])
            time.sleep(2)
    ################################MAIN WINDOW 1##################################################################################
    class MainWindow(QMainWindow):
        def __init__(self):
            super().__init__()
            self.windows = []
    
            l = QVBoxLayout()
            camera_controls_button = QPushButton("CAMERA CONTROLS")
            camera_controls_button.clicked.connect(self.open_newWindow)
            l.addWidget(camera_controls_button)
    
            w = QWidget()
            w.setLayout(l)
            self.setCentralWidget(w)
    
        def open_newWindow(self):
            window = MainWindow2()
            self.windows.append(window)
            window.show()
    
    ################################MAIN WINDOW 2##################################################################################
    
    class MainWindow2(QMainWindow):
        def __init__(self):
            super().__init__()
            self.windows = []
    
            l = QVBoxLayout()
            zoom_controls_button = QPushButton("ZOOM CONTROLS")
            zoom_controls_button.clicked.connect(self.open_newWindow2)
            l.addWidget(zoom_controls_button)
            record_controls_button = QPushButton("RECORDING")
            record_controls_button.clicked.connect(self.open_newWindow3)
            l.addWidget(record_controls_button)
    
            s = QWidget()
            s.setLayout(l)
            self.setCentralWidget(s)
    
        def open_newWindow2(self):
            window1 = zoom()
            self.windows.append(window1)
            window1.show()
            
        def open_newWindow3(self):
            window2 = recording()
            self.windows.append(window2)
            window2.show()
            
    ####################END#########################################################################################################
    app = QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()
    
    

    【讨论】:

      猜你喜欢
      • 2018-10-19
      • 2020-05-18
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-14
      • 1970-01-01
      • 2018-05-16
      • 1970-01-01
      相关资源
      最近更新 更多