class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Volume Evaluation") self.setFixedSize(600, 400) self.Volume_Evaluation() self.dial() def dial(self): self.widget = QDial(self) self.widget.setGeometry(100, 100, 150, 200) # x,y,w,h self.widget.setRange(0, 100) self.widget.setSingleStep(1) # set keyboard input to 1 step self.widget.setSliderPosition(0) # set pos to 0 self.widget.valueChanged.connect(self.value_changed) def Volume_Evaluation(self): self.text_label = QLabel("Move Dial to Evaluate", self) self.text_label.setGeometry(260, 150, 320, 100) # x,y,w,h self.text_label.setFont(QFont('Arial', 15)) def value_changed(self, i): if i == 0: self.text_label.setText(f"Current value[{i}]: Muted") if i > 0: self.text_label.setText(f"Current value[{i}]: Very Low") if i >= 20: self.text_label.setText(f"Current value[{i}]: Low") if i >= 45: self.text_label.setText(f"Current value[{i}]: Medium") if i >= 80: self.text_label.setText(f"Current value[{i}]: High") if i == 100: self.text_label.setText(f"Current value[{i}]: Max") print(i)
class MyMainWindow(QMainWindow): def __init__(self, ch=0, parent=None, name="Temperatur Simulation"): super().__init__(parent) self.setWindowTitle(name) self.__channel = ch cw = QWidget(self) layout = QVBoxLayout(cw) self.setCentralWidget(cw) self.__label = QLabel("Temperatur: ", self) self.__dial = QDial(self) self.__dial.setRange(100, 400) self.__dial.setSingleStep(1) self.__dial.setPageStep(10) self.__dial.setTracking(False) layout.addWidget(QLabel("Channel: {}".format(ch), self)) layout.addWidget(self.__label) layout.addWidget(self.__dial) self.__dial.valueChanged.connect(self.__sliderChanged) self.__dial.setValue(200) self.__sliderChanged() def __sliderChanged(self): temp = self.__dial.value() / 10 self.__label.setText("Temperatur: {:.1f}°".format(temp)) temp = temp * 4096.0 / 330 with open('/tmp/wiringPiSPI_{}'.format(self.__channel), 'w') as f: fcntl.flock(f, fcntl.LOCK_EX) f.write("{} {} {}".format(6, int(temp // 256), int(temp % 256))) fcntl.flock(f, fcntl.LOCK_UN)
def __init__(self): super().__init__() self.setWindowTitle("My App") widget = QDial() widget.setRange(-10, 100) widget.setSingleStep(0.5) widget.valueChanged.connect(self.value_changed) widget.sliderMoved.connect(self.slider_position) widget.sliderPressed.connect(self.slider_pressed) widget.sliderReleased.connect(self.slider_released) self.setCentralWidget(widget)
class MainWindow(QMainWindow): """Voice Changer main window.""" def __init__(self, parent=None): super(MainWindow, self).__init__() self.statusBar().showMessage("Move Dial to Deform Microphone Voice !.") self.setWindowTitle(__doc__) self.setMinimumSize(240, 240) self.setMaximumSize(480, 480) self.resize(self.minimumSize()) self.setWindowIcon(QIcon.fromTheme("audio-input-microphone")) self.tray = QSystemTrayIcon(self) self.center() QShortcut("Ctrl+q", self, activated=lambda: self.close()) self.menuBar().addMenu("&File").addAction("Quit", lambda: exit()) self.menuBar().addMenu("Sound").addAction( "STOP !", lambda: call('killall rec', shell=True)) windowMenu = self.menuBar().addMenu("&Window") windowMenu.addAction("Hide", lambda: self.hide()) windowMenu.addAction("Minimize", lambda: self.showMinimized()) windowMenu.addAction("Maximize", lambda: self.showMaximized()) windowMenu.addAction("Restore", lambda: self.showNormal()) windowMenu.addAction("FullScreen", lambda: self.showFullScreen()) windowMenu.addAction("Center", lambda: self.center()) windowMenu.addAction("Top-Left", lambda: self.move(0, 0)) windowMenu.addAction("To Mouse", lambda: self.move_to_mouse_position()) # widgets group0 = QGroupBox("Voice Deformation") self.setCentralWidget(group0) self.process = QProcess(self) self.process.error.connect( lambda: self.statusBar().showMessage("Info: Process Killed", 5000)) self.control = QDial() self.control.setRange(-10, 20) self.control.setSingleStep(5) self.control.setValue(0) self.control.setCursor(QCursor(Qt.OpenHandCursor)) self.control.sliderPressed.connect( lambda: self.control.setCursor(QCursor(Qt.ClosedHandCursor))) self.control.sliderReleased.connect( lambda: self.control.setCursor(QCursor(Qt.OpenHandCursor))) self.control.valueChanged.connect( lambda: self.control.setToolTip(f"<b>{self.control.value()}")) self.control.valueChanged.connect( lambda: self.statusBar().showMessage( f"Voice deformation: {self.control.value()}", 5000)) self.control.valueChanged.connect(self.run) self.control.valueChanged.connect(lambda: self.process.kill()) # Graphic effect self.glow = QGraphicsDropShadowEffect(self) self.glow.setOffset(0) self.glow.setBlurRadius(99) self.glow.setColor(QColor(99, 255, 255)) self.control.setGraphicsEffect(self.glow) self.glow.setEnabled(False) # Timer to start self.slider_timer = QTimer(self) self.slider_timer.setSingleShot(True) self.slider_timer.timeout.connect(self.on_slider_timer_timeout) # an icon and set focus QLabel(self.control).setPixmap( QIcon.fromTheme("audio-input-microphone").pixmap(32)) self.control.setFocus() QVBoxLayout(group0).addWidget(self.control) self.menu = QMenu(__doc__) self.menu.addAction(__doc__).setDisabled(True) self.menu.setIcon(self.windowIcon()) self.menu.addSeparator() self.menu.addAction( "Show / Hide", lambda: self.hide() if self.isVisible() else self.showNormal()) self.menu.addAction("STOP !", lambda: call('killall rec', shell=True)) self.menu.addSeparator() self.menu.addAction("Quit", lambda: exit()) self.tray.setContextMenu(self.menu) self.make_trayicon() def run(self): """Run/Stop the QTimer.""" if self.slider_timer.isActive(): self.slider_timer.stop() self.glow.setEnabled(True) call('killall rec ; killall play', shell=True) self.slider_timer.start(3000) def on_slider_timer_timeout(self): """Run subprocess to deform voice.""" self.glow.setEnabled(False) value = int(self.control.value()) * 100 command = f'play -q -V0 "|rec -q -V0 -n -d -R riaa bend pitch {value} "' print(f"Voice Deformation Value: {value}") print(f"Voice Deformation Command: {command}") self.process.start(command) if self.isVisible(): self.statusBar().showMessage("Minimizing to System TrayIcon", 3000) print("Minimizing Main Window to System TrayIcon now...") sleep(3) self.hide() def center(self): """Center Window on the Current Screen,with Multi-Monitor support.""" window_geometry = self.frameGeometry() mousepointer_position = QApplication.desktop().cursor().pos() screen = QApplication.desktop().screenNumber(mousepointer_position) centerPoint = QApplication.desktop().screenGeometry(screen).center() window_geometry.moveCenter(centerPoint) self.move(window_geometry.topLeft()) def move_to_mouse_position(self): """Center the Window on the Current Mouse position.""" window_geometry = self.frameGeometry() window_geometry.moveCenter(QApplication.desktop().cursor().pos()) self.move(window_geometry.topLeft()) def make_trayicon(self): """Make a Tray Icon.""" if self.windowIcon() and __doc__: self.tray.setIcon(self.windowIcon()) self.tray.setToolTip(__doc__) self.tray.activated.connect( lambda: self.hide() if self.isVisible() else self.showNormal()) return self.tray.show()
class MainWindow(QMainWindow): """Voice Changer main window.""" def __init__(self, parent=None): super(MainWindow, self).__init__() self.statusBar().showMessage("Move Dial to Deform Microphone Voice !.") self.setWindowTitle(__doc__) self.setMinimumSize(240, 240) self.setMaximumSize(480, 480) self.resize(self.minimumSize()) self.setWindowIcon(QIcon.fromTheme("audio-input-microphone")) self.tray = QSystemTrayIcon(self) self.center() QShortcut("Ctrl+q", self, activated=lambda: self.close()) self.menuBar().addMenu("&File").addAction("Quit", lambda: exit()) self.menuBar().addMenu("Sound").addAction( "STOP !", lambda: call('killall rec', shell=True)) windowMenu = self.menuBar().addMenu("&Window") windowMenu.addAction("Hide", lambda: self.hide()) windowMenu.addAction("Minimize", lambda: self.showMinimized()) windowMenu.addAction("Maximize", lambda: self.showMaximized()) windowMenu.addAction("Restore", lambda: self.showNormal()) windowMenu.addAction("FullScreen", lambda: self.showFullScreen()) windowMenu.addAction("Center", lambda: self.center()) windowMenu.addAction("Top-Left", lambda: self.move(0, 0)) windowMenu.addAction("To Mouse", lambda: self.move_to_mouse_position()) # widgets group0 = QGroupBox("Voice Deformation") self.setCentralWidget(group0) self.process = QProcess(self) self.process.error.connect( lambda: self.statusBar().showMessage("Info: Process Killed", 5000)) self.control = QDial() self.control.setRange(-10, 20) self.control.setSingleStep(5) self.control.setValue(0) self.control.setCursor(QCursor(Qt.OpenHandCursor)) self.control.sliderPressed.connect( lambda: self.control.setCursor(QCursor(Qt.ClosedHandCursor))) self.control.sliderReleased.connect( lambda: self.control.setCursor(QCursor(Qt.OpenHandCursor))) self.control.valueChanged.connect( lambda: self.control.setToolTip("<b>" + str(self.control.value()))) self.control.valueChanged.connect(lambda: self.statusBar().showMessage( "Voice deformation: " + str(self.control.value()), 5000)) self.control.valueChanged.connect(self.run) self.control.valueChanged.connect(lambda: self.process.kill()) # Graphic effect self.glow = QGraphicsDropShadowEffect(self) self.glow.setOffset(0) self.glow.setBlurRadius(99) self.glow.setColor(QColor(99, 255, 255)) self.control.setGraphicsEffect(self.glow) self.glow.setEnabled(False) # Timer to start self.slider_timer = QTimer(self) self.slider_timer.setSingleShot(True) self.slider_timer.timeout.connect(self.on_slider_timer_timeout) # an icon and set focus QLabel(self.control).setPixmap( QIcon.fromTheme("audio-input-microphone").pixmap(32)) self.control.setFocus() QVBoxLayout(group0).addWidget(self.control) self.menu = QMenu(__doc__) self.menu.addAction(__doc__).setDisabled(True) self.menu.setIcon(self.windowIcon()) self.menu.addSeparator() self.menu.addAction( "Show / Hide", lambda: self.hide() if self.isVisible() else self.showNormal()) self.menu.addAction("STOP !", lambda: call('killall rec', shell=True)) self.menu.addSeparator() self.menu.addAction("Quit", lambda: exit()) self.tray.setContextMenu(self.menu) self.make_trayicon() def run(self): """Run/Stop the QTimer.""" if self.slider_timer.isActive(): self.slider_timer.stop() self.glow.setEnabled(True) call('killall rec', shell=True) self.slider_timer.start(3000) def on_slider_timer_timeout(self): """Run subprocess to deform voice.""" self.glow.setEnabled(False) value = int(self.control.value()) * 100 cmd = 'play -q -V0 "|rec -q -V0 -n -d -R riaa bend pitch {0} "' command = cmd.format(int(value)) log.debug("Voice Deformation Value: {0}".format(value)) log.debug("Voice Deformation Command: {0}".format(command)) self.process.start(command) if self.isVisible(): self.statusBar().showMessage("Minimizing to System TrayIcon", 3000) log.debug("Minimizing Main Window to System TrayIcon now...") sleep(3) self.hide() def center(self): """Center Window on the Current Screen,with Multi-Monitor support.""" window_geometry = self.frameGeometry() mousepointer_position = QApplication.desktop().cursor().pos() screen = QApplication.desktop().screenNumber(mousepointer_position) centerPoint = QApplication.desktop().screenGeometry(screen).center() window_geometry.moveCenter(centerPoint) self.move(window_geometry.topLeft()) def move_to_mouse_position(self): """Center the Window on the Current Mouse position.""" window_geometry = self.frameGeometry() window_geometry.moveCenter(QApplication.desktop().cursor().pos()) self.move(window_geometry.topLeft()) def make_trayicon(self): """Make a Tray Icon.""" if self.windowIcon() and __doc__: self.tray.setIcon(self.windowIcon()) self.tray.setToolTip(__doc__) self.tray.activated.connect( lambda: self.hide() if self.isVisible() else self.showNormal()) return self.tray.show()
class SpinnerDialComboWidget(QWidget): value_changed = pyqtSignal() # name: The string name that will be displayed on top of the widget # default_value: The value that will be initially set as the widget's value # min_val: The minimum value that will be initially set # max_val: The maximum value that will be initially set def __init__(self, name="", default_value=0, min_val=0, max_val=100, parent=None): QWidget.__init__(self, parent=parent) # The minimum value that can be set self.min_val = min_val # The maximum value that can be set self.max_val = max_val # The widget's current value self.value = default_value self.title_label = QLabel(name) # The widget's dial self.dial = QDial(self) self.dial.setSingleStep(1) self.dial.setPageStep(1) self.dial.setMinimum(min_val) self.dial.setMaximum(max_val) self.dial.setValue(default_value) self.dial.valueChanged.connect(self.on_dial_changed) # The widget's spin box self.spinner = QSpinBox(self) self.spinner.setMinimum(min_val) self.spinner.setMaximum(max_val) self.spinner.setValue(default_value) self.spinner.valueChanged.connect(self.on_spinner_changed) self.setup_gui() # Sets up the positioning of the UI elements def setup_gui(self): vertical_layout = QVBoxLayout(self) vertical_layout.addStretch(1) vertical_layout.addWidget(self.title_label) vertical_layout.addWidget(self.spinner) vertical_layout.addWidget(self.dial) # The callback for when the dial is changes @pyqtSlot() def on_dial_changed(self): self.value = self.dial.value() self.spinner.blockSignals(True) self.spinner.setValue(self.dial.value()) self.spinner.blockSignals(False) self.value_changed.emit() # The callback for when the spin box is changed @pyqtSlot() def on_spinner_changed(self): self.value = self.spinner.value() self.dial.blockSignals(True) self.dial.setValue(self.spinner.value()) self.dial.blockSignals(False) self.value_changed.emit() # Sets the minimum value # new_min: The new minimum value to be set def set_min(self, new_min): if new_min > self.max_val: return self.min_val = new_min self.dial.blockSignals(True) self.spinner.blockSignals(True) self.spinner.setMinimum(new_min) self.dial.setMinimum(new_min) self.dial.blockSignals(False) self.spinner.blockSignals(False) self.value_changed.emit() # Sets the maximum value # new_max: The new maximum value to be set def set_max(self, new_max): if new_max < self.min_val: return self.max_val = new_max self.dial.blockSignals(True) self.spinner.blockSignals(True) self.spinner.setMaximum(new_max) self.dial.setMaximum(new_max) self.dial.blockSignals(False) self.spinner.blockSignals(False) self.value_changed.emit() # Sets the widget value # value: The value to be set def set_value(self, value): self.value = value self.dial.blockSignals(True) self.spinner.blockSignals(True) self.dial.setValue(value) self.spinner.setValue(value) self.dial.blockSignals(False) self.spinner.blockSignals(False) self.value_changed.emit()
class MainGUI(QDialog): def __init__(self, parent=None, imageoperator=None): super(MainGUI, self).__init__(parent) self.inputfile = None self.batchfilenames = None self.imageoperator = imageoperator self.originalPalette = QApplication.palette() self.btnFileOpen = QPushButton("Choose image file...") self.btnFileOpen.clicked.connect(self.getfile) self.leInputImage = QLabel() self.leInputImage.setPixmap( QPixmap( os.path.abspath( os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', '..', 'resources', 'emptyspace.png'))).scaledToHeight(400)) self.leOutputImage = QLabel() self.leOutputImage.setPixmap( QPixmap( os.path.abspath( os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', '..', '..', 'resources', 'emptyspace.png'))).scaledToHeight(400)) self.createBottomLeftTabWidget() self.createBottomRightTabWidget() self.createProgressBar() # Top row of GUI, with image displays topLeftLayout = QGroupBox("Input Image") layout = QVBoxLayout() layout.addWidget(self.leInputImage) layout.addWidget(self.btnFileOpen) layout.addStretch(1) topLeftLayout.setLayout(layout) topRightLayout = QGroupBox("Output Image") layout = QVBoxLayout() layout.addWidget(self.leOutputImage) layout.addStretch(1) topRightLayout.setLayout(layout) topLayout = QHBoxLayout() topLayout.addWidget(topLeftLayout) topLayout.addWidget(topRightLayout) # Bottom row of GUI, with processing functions bottomLeftLayout = QGroupBox("Processing") layout = QVBoxLayout() layout.addWidget(self.bottomLeftTabWidget) layout.addStretch(1) bottomLeftLayout.setLayout(layout) bottomRightLayout = QGroupBox("Results") layout = QVBoxLayout() layout.addWidget(self.bottomRightTabWidget) layout.addStretch(1) bottomRightLayout.setLayout(layout) bottomLayout = QHBoxLayout() bottomLayout.addWidget(bottomLeftLayout) bottomLayout.addWidget(bottomRightLayout) mainLayout = QGridLayout() mainLayout.addLayout(topLayout, 0, 0, 1, 2) mainLayout.addLayout(bottomLayout, 1, 0, 1, 2) mainLayout.addWidget(self.bottomLeftTabWidget, 1, 0) mainLayout.addWidget(self.bottomRightTabWidget, 1, 1) mainLayout.addWidget(self.progressBar, 3, 0, 1, 2) mainLayout.setRowStretch(0, 1) mainLayout.setRowStretch(1, 1) mainLayout.setRowMinimumHeight(1, 200) mainLayout.setColumnStretch(0, 1) mainLayout.setColumnStretch(1, 1) self.setLayout(mainLayout) self.setWindowTitle("Pituitary Cytokeratin Spatial Frequency") QApplication.setStyle(QStyleFactory.create('Fusion')) QApplication.setPalette(QApplication.style().standardPalette()) def createBottomLeftTabWidget(self): self.bottomLeftTabWidget = QTabWidget() self.bottomLeftTabWidget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Ignored) tab1 = QWidget() self.btnProcess = QPushButton("Process!") self.btnProcess.setStyleSheet( "font: bold;background-color: green;font-size: 36px;height: 48px;width: 300px;" ) self.btnProcess.clicked.connect(self.processInputImage) self.dial = QDial() self.dial.setMinimum(1) self.dial.setMaximum(20) self.dial.setValue(6) self.dial.setSingleStep(1) self.dial.setNotchesVisible(True) self.dial.valueChanged.connect(self.handleDialMove) self.SpaceConstLabel = QLabel() self.SpaceConstLabel.setText("Space Constant: " + str(self.dial.value())) tab1hbox = QHBoxLayout() tab1hbox.setContentsMargins(5, 5, 5, 5) tab1hbox.addStretch(0) tab1hbox.addWidget(self.btnProcess) tab1hbox.addStretch(0) tab1hbox.addWidget(self.dial) tab1hbox.addWidget(self.SpaceConstLabel) tab1hbox.addStretch(0) tab1.setLayout(tab1hbox) tab2 = QWidget() self.batchTableWidget = QTableWidget(10, 1) self.batchTableWidget.setHorizontalHeaderLabels(["Filename"]) header = self.batchTableWidget.horizontalHeader() header.setSectionResizeMode(0, QHeaderView.Stretch) tab2hbox = QHBoxLayout() tab2hbox.setContentsMargins(5, 5, 5, 5) tab2hbox.addWidget(self.batchTableWidget) self.buttonBatchLoad = QPushButton("Load Files") self.buttonBatchLoad.clicked.connect(self.handleBatchLoad) tab2hbox.addWidget(self.buttonBatchLoad) tab2.setLayout(tab2hbox) self.bottomLeftTabWidget.addTab(tab1, "&Processing") self.bottomLeftTabWidget.addTab(tab2, "&Batch") def createBottomRightTabWidget(self): self.bottomRightTabWidget = QTabWidget() self.bottomRightTabWidget.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Ignored) tab1 = QWidget() self.tableWidget = QTableWidget(10, 2) self.tableWidget.setHorizontalHeaderLabels( ["Filename", "Density Index"]) header = self.tableWidget.horizontalHeader() header.setSectionResizeMode(0, QHeaderView.Stretch) header.setSectionResizeMode(1, QHeaderView.ResizeToContents) self.TableRowCursor = 0 tab1hbox = QHBoxLayout() tab1hbox.setContentsMargins(5, 5, 5, 5) tab1hbox.addWidget(self.tableWidget) self.buttonSave = QPushButton("Save CSV") self.buttonSave.clicked.connect(self.handleSave) tab1hbox.addWidget(self.buttonSave) tab1.setLayout(tab1hbox) tab2 = QWidget() textEdit = QTextEdit() textEdit.setPlainText( "The Magi\n" "W. B. Yeats - 1865-1939\n" "\n" "Now as at all times I can see in the mind's eye,\n" "In their stiff, painted clothes, the pale unsatisfied ones\n" "Appear and disappear in the blue depth of the sky\n" "With all their ancient faces like rain-beaten stones,\n" "And all their helms of silver hovering side by side,\n" "And all their eyes still fixed, hoping to find once more,\n" "Being by Calvary's turbulence unsatisfied,\n" "The uncontrollable mystery on the bestial floor.\n") tab2hbox = QHBoxLayout() tab2hbox.setContentsMargins(5, 5, 5, 5) tab2hbox.addWidget(textEdit) tab2.setLayout(tab2hbox) self.bottomRightTabWidget.addTab(tab1, "&Results") self.bottomRightTabWidget.addTab(tab2, "Free &Text") def createProgressBar(self): self.progressBar = QProgressBar() self.progressBar.setRange(0, 10000) self.progressBar.setValue(0) def advanceProgressBar(self): curVal = self.progressBar.value() maxVal = self.progressBar.maximum() self.progressBar.setValue(curVal + (maxVal - curVal) / 100) def getfile(self): self.inputfname = QFileDialog.getOpenFileName(self, 'Open file', '~', "Image files (*.*)") if os.path.isfile(self.inputfname[0]): self.inputfile = self.inputfname[0] self.leInputImage.setPixmap( QPixmap(self.inputfile).scaledToHeight(400)) def handleDialMove(self): self.SpaceConstLabel.setText("Space Constant: " + str(self.dial.value())) def handleBatchLoad(self): userlist = QFileDialog.getOpenFileNames(self, 'Open file', '~', "Image files (*.*)") self.batchfilenames = userlist[0] self.batchTableWidget.setRowCount(len(self.batchfilenames)) self.batchTableWidget.clear() for row in range(len(self.batchfilenames)): self.inputfile = None self.batchTableWidget.setItem( row - 1, 1, QTableWidgetItem(os.path.basename(self.batchfilenames[row]))) def processInputImage(self): if (self.inputfile): filelist = [self.inputfile] display_output_image = True elif (self.batchfilenames): filelist = self.batchfilenames display_output_image = False else: filelist = [] print("No input file(s) specified!") return (0) self.imageoperator.setlims([self.dial.value(), 10 * self.dial.value()]) self.progressBar.setRange(0, len(filelist)) self.progressBar.setValue(0) for row in range(len(filelist)): infl = filelist[row] r = self.imageoperator.processImage(infl) di = r['density_index'] if (display_output_image): imout = np.int8( np.floor(255 * np.stack((r['bpdiffim'], ) * 3, axis=-1))) h, w, c = imout.shape bytesPerLine = w * 3 qpix = QPixmap.fromImage( QImage(imout, w, h, bytesPerLine, QImage.Format_RGB888)) self.leOutputImage.setPixmap(qpix.scaledToHeight(400)) #print("Density index: {0:.2f}".format(di)) nr = self.tableWidget.rowCount() if nr <= self.TableRowCursor: self.tableWidget.insertRow(nr) self.tableWidget.setItem(self.TableRowCursor, 0, QTableWidgetItem(os.path.basename(infl))) self.tableWidget.setItem(self.TableRowCursor, 1, QTableWidgetItem(str(di))) self.TableRowCursor = self.TableRowCursor + 1 self.progressBar.setValue(row + 1) def handleSave(self): p = QFileDialog.getSaveFileName(self, 'Save File', '', 'CSV(*.csv)') path = p[0] if len(path): with open(path, 'w') as stream: writer = csv.writer(stream) for row in range(self.tableWidget.rowCount()): rowdata = [] emptyrow = True for column in range(self.tableWidget.columnCount()): item = self.tableWidget.item(row, column) if item is not None: rowdata.append(item.text()) emptyrow = False else: rowdata.append('') if not emptyrow: writer.writerow(rowdata)
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Homer") # Fuel rods self.fuel_rod_label = QLabel("Fuel Rod Position") self.fuel_rod_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.fuel_rod_position_gauge = QDial() self.fuel_rod_position_gauge.setRange(0,100) self.fuel_rod_position_gauge.setSingleStep(1) self.fuel_rod_position_gauge.setNotchesVisible(True) self.fuel_rod_target_label = QLabel("Fuel Rod Position Target") self.fuel_rod_target_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.fuel_rod_position = QSpinBox() self.fuel_rod_position.setMinimum(0) self.fuel_rod_position.setMaximum(100) self.fuel_rod_position.setSingleStep(1) self.fuel_rod_position.valueChanged.connect(self.fuel_rod_position_changed) fuel_rod_layout = QVBoxLayout() fuel_rod_layout.setAlignment(Qt.AlignHCenter | Qt.AlignTop) fuel_rod_layout.addWidget(self.fuel_rod_label) fuel_rod_layout.addWidget(self.fuel_rod_position_gauge) fuel_rod_layout.addWidget(self.fuel_rod_target_label) fuel_rod_layout.addWidget(self.fuel_rod_position) fuel_rod_widget = QWidget() fuel_rod_widget.setLayout(fuel_rod_layout) # Primary loop controls self.primary_temp_label = QLabel("Primary Coolant Temp") self.primary_temp_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.primary_temp_gauge = QDial() self.primary_temp_gauge.setRange(0,100) self.primary_temp_gauge.setSingleStep(1) self.primary_temp_gauge.setNotchesVisible(True) self.primary_temp_gauge.valueChanged.connect(self.primary_temp_value_changed) self.primary_pressure_label = QLabel("Primary Coolant Pressure") self.primary_pressure_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.primary_pressure_gauge = QDial() self.primary_pressure_gauge.setRange(0,100) self.primary_pressure_gauge.setSingleStep(1) self.primary_pressure_gauge.setNotchesVisible(True) self.primary_pump_rpm_label = QLabel("Primary Pump RPM") self.primary_pump_rpm_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.primary_pump_rpm = QSpinBox() self.primary_pump_rpm.setMinimum(0) self.primary_pump_rpm.setMaximum(100) self.primary_pump_rpm.setSingleStep(1) self.primary_pump_rpm.valueChanged.connect(self.primary_pump_rpm_changed) self.primary_relief_valve = QPushButton("vent primary") self.primary_relief_valve.setCheckable(True) self.primary_relief_valve.clicked.connect(self.primary_relief_valve_clicked) primary_loop_layout = QVBoxLayout() primary_loop_layout.setAlignment(Qt.AlignHCenter | Qt.AlignTop) primary_loop_layout.addWidget(self.primary_temp_label) primary_loop_layout.addWidget(self.primary_temp_gauge) primary_loop_layout.addWidget(self.primary_pressure_label) primary_loop_layout.addWidget(self.primary_pressure_gauge) primary_loop_layout.addWidget(self.primary_pump_rpm_label) primary_loop_layout.addWidget(self.primary_pump_rpm) primary_loop_layout.addWidget(self.primary_relief_valve) primary_loop_widget = QWidget() primary_loop_widget.setLayout(primary_loop_layout) # Secondary loop controls self.secondary_temp_label = QLabel("Secondary Coolant Temp") self.secondary_temp_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.secondary_temp_gauge = QDial() self.secondary_temp_gauge.setRange(0,100) self.secondary_temp_gauge.setSingleStep(1) self.secondary_temp_gauge.setNotchesVisible(True) self.secondary_temp_gauge.valueChanged.connect(self.secondary_temp_value_changed) self.secondary_pressure_label = QLabel("Secondary Coolant Pressure") self.secondary_pressure_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.secondary_pressure_gauge = QDial() self.secondary_pressure_gauge.setRange(0,100) self.secondary_pressure_gauge.setSingleStep(1) self.secondary_pressure_gauge.setNotchesVisible(True) self.secondary_pump_rpm_label = QLabel("Secondary Pump RPM") self.secondary_pump_rpm_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.secondary_pump_rpm = QSpinBox() self.secondary_pump_rpm.setMinimum(0) self.secondary_pump_rpm.setMaximum(100) self.secondary_pump_rpm.setSingleStep(1) self.secondary_pump_rpm.valueChanged.connect(self.secondary_pump_rpm_changed) self.secondary_relief_valve = QPushButton("vent secondary") self.secondary_relief_valve.setCheckable(True) self.secondary_relief_valve.clicked.connect(self.secondary_relief_valve_clicked) secondary_loop_layout = QVBoxLayout() secondary_loop_layout.setAlignment(Qt.AlignHCenter | Qt.AlignTop) secondary_loop_layout.addWidget(self.secondary_temp_label) secondary_loop_layout.addWidget(self.secondary_temp_gauge) secondary_loop_layout.addWidget(self.secondary_pressure_label) secondary_loop_layout.addWidget(self.secondary_pressure_gauge) secondary_loop_layout.addWidget(self.secondary_pump_rpm_label) secondary_loop_layout.addWidget(self.secondary_pump_rpm) secondary_loop_layout.addWidget(self.secondary_relief_valve) secondary_loop_widget = QWidget() secondary_loop_widget.setLayout(secondary_loop_layout) # Turbine/generator self.turbine_rpm_label = QLabel("Turbine RPM") self.turbine_rpm_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.turbine_rpm_gauge = QDial() self.turbine_rpm_gauge.setRange(0,100) self.turbine_rpm_gauge.setSingleStep(1) self.turbine_rpm_gauge.setNotchesVisible(True) self.generator_current_label = QLabel("Generator Current") self.generator_current_label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) self.generator_current_gauge = QDial() self.generator_current_gauge.setRange(0,100) self.generator_current_gauge.setSingleStep(1) self.generator_current_gauge.setNotchesVisible(True) turbine_layout = QVBoxLayout() turbine_layout.setAlignment(Qt.AlignHCenter | Qt.AlignTop) turbine_layout.addWidget(self.turbine_rpm_label) turbine_layout.addWidget(self.turbine_rpm_gauge) turbine_layout.addWidget(self.generator_current_label) turbine_layout.addWidget(self.generator_current_gauge) turbine_layout_widget = QWidget() turbine_layout_widget.setLayout(turbine_layout) # Layout all the controls main_layout = QGridLayout() # Fuel rods main_layout.addWidget(fuel_rod_widget, 0,0) # Primary loop main_layout.addWidget(primary_loop_widget, 0, 1) # Secondary loop main_layout.addWidget(secondary_loop_widget,0,2) # TODO: Condenser # Turbine main_layout.addWidget(turbine_layout_widget,0,3) main_widget = QWidget() main_widget.setLayout(main_layout) self.setCentralWidget(main_widget) # timer self.timer = QTimer() self.timer.setInterval(1000) self.timer.timeout.connect(self.recurring_timer) self.timer.start() # Event handlers def recurring_timer(self): reactor.tick() self.fuel_rod_position_gauge.setValue(reactor.rod_position) self.primary_temp_gauge.setValue(reactor.primary_temp) self.primary_pressure_gauge.setValue(reactor.primary_pressure) self.secondary_temp_gauge.setValue(reactor.secondary_temp) self.secondary_pressure_gauge.setValue(reactor.secondary_pressure) self.turbine_rpm_gauge.setValue(reactor.turbine_rpm) self.generator_current_gauge.setValue(reactor.generator_current) def fuel_rod_position_changed(self, i): reactor.set_rod_position(i) def primary_temp_value_changed(self, i): print(i) def primary_relief_valve_clicked(self, checked): print(checked) if checked: reactor.open_primary_relief_valve() else: reactor.close_primary_relief_valve() def primary_pump_rpm_changed(self, i): reactor.set_primary_pump_rpm(i) def secondary_temp_value_changed(self, i): print(i) def secondary_relief_valve_clicked(self, checked): print(checked) if checked: reactor.open_secondary_relief_valve() else: reactor.close_secondary_relief_valve() def secondary_pump_rpm_changed(self, i): reactor.set_secondary_pump_rpm(i)