【发布时间】: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