def initUI(self): #set the main widget's tooltip # QToolTip.setFont(QFont('Sansserif',9)) # self.setToolTip('this is a <b>QWidget</b> widget') #set the button # btn = QPushButton('Button',self) # btn.move(150,250) # btn.resize(btn.sizeHint()) # btn.clicked.connect(QCoreApplication.instance().quit) # btn.setToolTip('this is a <b>QPushButton</b> button') #set the label # label1 = QLabel('label1',self) # label1.move(15,100) # label2 = QLabel('label2',self) # label2.move(35,200) #set the textEdit # texteEdit = QTextEdit() # self.setCentralWidget(texteEdit) #set the window center self.resize(550,350) self.setWindowTitle('Hello,PyQt5') self.setWindowIcon(QIcon('./resources/Icon.png')) #set the status bar # self.statusBar().showMessage('Ready') #set the menubar exitAction = QAction(QIcon('./resources/exit.jpg'),'&Exit',self) exitAction.setShortcut('Ctrl+Q') exitAction.setStatusTip('Exit the application') exitAction.triggered.connect(qApp.quit) # menuBar = self.menuBar() # fileBar = menuBar.addMenu('&File') # fileBar.addAction(exitAction) #set the toolbar # self.addToolBar('Exit').addAction(exitAction) #set the Layout okButton = QPushButton('OK') cancelButton = QPushButton('Cancel') hbox = QHBoxLayout() hbox.stretch(1) hbox.addWidget(okButton) hbox.addWidget(cancelButton) vbox = QVBoxLayout() vbox.stretch(1) vbox.addLayout(hbox) self.setLayout(vbox) self.center() self.show()
def init_ui(self): self.text_label = QLabel("There has been no name entered, so I can do anything yet.") self.label = QLabel("Name: ") self.name_input = QLineEdit() self.button = QPushButton("Clicked: 0 ") self.button.setText("Set Name") self.button.clicked.connect(self.alterName) h = QHBoxLayout() h.stretch(1) h.addWidget(self.label) h.addWidget(self.name_input) v = QVBoxLayout() v.addWidget(self.text_label) v.addLayout(h) v.addWidget(self.button) self.setLayout(v) self.setWindowTitle("Nothing has been clicked") self.show()
class Window(QMainWindow): def __init__(self): super(Window, self).__init__() self.initUI() def initUI(self): # Set up window self.setFixedSize(900, 900) self.setWindowTitle("Buffon Needle Simulator") # Set up main widget self.widget = QWidget() self.setCentralWidget(self.widget) # Set up layouts self.hbox = QHBoxLayout() self.hbox.setAlignment(QtCore.Qt.AlignTop) self.vbox = QVBoxLayout() self.vbox.addLayout(self.hbox) self.vbox.setAlignment(QtCore.Qt.AlignTop) self.widget.setLayout(self.vbox) # type selection layout self.t = 'Needle' self.typeSelection = QHBoxLayout() self.typeLabel = QLabel('Type: ') self.typeSelection.addWidget(self.typeLabel) self.needleType = QRadioButton('Needle') self.needleType.setChecked(True) self.needleType.toggled.connect( lambda: self.changeType(self.needleType)) self.typeSelection.addWidget(self.needleType) self.circleType = QRadioButton('Circle') self.circleType.toggled.connect( lambda: self.changeType(self.circleType)) self.typeSelection.addWidget(self.circleType) self.typeSelection.addStretch() self.hbox.addLayout(self.typeSelection) # iterations label and combo box self.selectionSection = QHBoxLayout() self.iterationsLabel = QLabel("Amount:") self.selectionSection.addWidget(self.iterationsLabel) self.iterations = QComboBox() self.iterations.addItem('10') self.iterations.addItem('100') self.iterations.addItem('200') self.iterations.addItem('500') self.iterations.addItem('1000') self.iterations.addItem('2500') self.selectionSection.addWidget(self.iterations) # length label and combo box self.lengthLabel = QLabel("Length/Diameter:") self.selectionSection.addWidget(self.lengthLabel) self.length = QComboBox() self.length.move(150, 30) self.length.addItem('.1') self.length.addItem('.2') self.length.addItem('.3') self.length.addItem('.4') self.length.addItem('.5') self.length.addItem('.6') self.length.addItem('.7') self.length.addItem('.8') self.length.addItem('.9') self.length.addItem('1') self.selectionSection.addWidget(self.length) # Start simulation button self.start = QPushButton("Start Simulation") self.start.clicked.connect(self.simulation) self.selectionSection.addWidget(self.start) # Pause button self.pause = QPushButton('Pause') self.pause.setDisabled(True) self.pause.clicked.connect(self.pauseAction) self.pausing = False self.selectionSection.addWidget(self.pause) # Cancel button self.cancel = QPushButton('Cancel Simulation') self.cancel.setDisabled(True) self.cancel.clicked.connect(self.cancelAction) self.selectionSection.addWidget(self.cancel) # Add second row to vbox self.vbox.addLayout(self.selectionSection) # Stats section self.counter = 0 self.counterLabel = QLabel('Number of Plotted: 0') self.counterLabel.setFixedWidth(150) self.crossCounter = 0 self.crossLabel = QLabel('Number of Crossing: 0 (0.0%)') self.statsSection = QHBoxLayout() self.statsSection.setAlignment(QtCore.Qt.AlignLeft) self.statsSection.stretch(1) self.statsSection.addWidget(self.counterLabel) self.statsSection.addWidget(self.crossLabel) self.vbox.addLayout(self.statsSection) # Set up canvas self.canvas = MplCanvas(self, width=10, height=10, dpi=100) self.vbox.addWidget(self.canvas) self.canvas.axes.set_ylim([-0.5, 5.5]) self.canvas.axes.set_xlim([-0.5, 5.5]) x = [0, 1, 2, 3, 4, 5] for p in x: self.canvas.axes.axvline(p) self.show() def changeType(self, type): if type.text() == 'Needle' and type.isChecked(): self.t = 'Needle' elif type.text() == 'Circle' and type.isChecked(): self.t = "Circle" else: return def pauseAction(self): if self.pausing: self.timer.start() self.pausing = False self.pause.setText('Pause') else: self.timer.stop() self.pausing = True self.pause.setText('Resume') def cancelAction(self): self.timer.stop() self.length.setDisabled(False) self.iterations.setDisabled(False) self.pause.setDisabled(True) self.cancel.setDisabled(True) self.needleType.setDisabled(False) self.circleType.setDisabled(False) def simulation(self): # Set up for simulation self.start.setText('Restart Simulation') self.pause.setDisabled(False) self.pause.setText('Pause') self.cancel.setDisabled(False) self.counter = 0 self.crossCounter = 0 self.counterLabel.setText('Number of Plotted: 0') self.crossLabel.setText('Number of Crossing: 0 (0.0%)') self.length.setDisabled(True) self.iterations.setDisabled(True) self.circleType.setDisabled(True) self.needleType.setDisabled(True) self.canvas.axes.cla() self.xpoints = [0, 1, 2, 3, 4, 5] for p in self.xpoints: self.canvas.axes.axvline(p) cross = mpatches.Patch(color='green', label='Cross') miss = mpatches.Patch(color='red', label='Miss') self.canvas.axes.legend(handles=[cross, miss], loc='upper right', bbox_to_anchor=(1, 1.1)) self.canvas.axes.set_ylim([-0.5, 5.5]) self.canvas.axes.set_xlim([-0.5, 5.5]) self.total = int(self.iterations.currentText()) # Use timer to repeat plotting needles self.timer = QtCore.QTimer() self.timer.setInterval(5) self.timer.timeout.connect(self.plot) self.timer.start() self.canvas.draw() def plot(self): # if finish simulation if self.counter == self.total - 1: self.timer.stop() self.pause.setDisabled(True) self.length.setDisabled(False) self.iterations.setDisabled(False) self.cancel.setDisabled(True) self.needleType.setDisabled(False) self.circleType.setDisabled(False) self.start.setText('Start Simulation') # Updating ui and variables self.counter += 1 self.counterLabel.setText('Number of Plotted: ' + str(self.counter)) # Create a new needle and check crossing if self.t == 'Needle': n = Needle(float(self.length.currentText())) if n.check_cross(self.xpoints): self.canvas.axes.plot([n.start[0], n.end[0]], [n.start[1], n.end[1]], 'g') self.crossCounter += 1 else: self.canvas.axes.plot([n.start[0], n.end[0]], [n.start[1], n.end[1]], 'r') elif self.t == 'Circle': n = Circle(float(self.length.currentText())) if n.check_cross(self.xpoints): c = mpatches.Circle((n.center[0], n.center[1]), n.r, color='g') self.canvas.axes.add_patch(c) self.crossCounter += 1 else: c = mpatches.Circle((n.center[0], n.center[1]), n.r, color='r') self.canvas.axes.add_patch(c) else: print('invalid type') return self.crossLabel.setText( 'Number of Crossing: ' + str(self.crossCounter) + ' (' + str(round((self.crossCounter / self.counter) * 100, 2)) + '%)') self.canvas.draw()
def __init__(self, dc): # Initialize the object as a QWidget QWidget.__init__(self) #Save the datacollection object as an attribute of class StatsGui self.dc = dc #Set the title of the main GUI window self.setWindowTitle('Statistics') # Set up dicts for row indices self.subset_dict = dict() self.component_dict = dict() self.selected_dict = dict() self.selected_indices = [] #Set up tree view and fix it to the top half of the window self.treeview = QTreeView(self) # Set the default clicking behavior to be row selection self.treeview.setSelectionBehavior(QAbstractItemView.SelectRows) # Set up expand all, collapse all, select all and deselect all buttons # Layout for expand/collapse/select/deselect layout_left_options = QHBoxLayout() self.expand_data = QToolButton(self) self.expand_data.setText("Expand all data and subsets") self.expand_data.clicked.connect(self.expandClicked) layout_left_options.addWidget(self.expand_data) self.all = QToolButton(self) self.all.setText('Select all') self.all.clicked.connect(self.allClicked) layout_left_options.addWidget(self.all) self.none = QToolButton(self) self.none.setText('Deselect all') self.none.clicked.connect(self.noneClicked) layout_left_options.addWidget(self.none) # Set default significant figures to 5 getcontext().prec = 5 # Set up past selected items self.past_selected = [] # Sort by subsets as a default self.sortBySubsets() # Set up the combo box for users to choose the number of significant figures in the table # Set up bottom options layout layout_bottom_options = QHBoxLayout() self.siglabel = QLabel(self) self.siglabel.setText('Number of significant figures:') layout_bottom_options.addWidget(self.siglabel) self.sigfig = QSpinBox(self) self.sigfig.setRange(1, 10) self.sigfig.setValue(5) self.sigfig.valueChanged.connect(self.sigchange) layout_bottom_options.addWidget(self.sigfig) # Export to file button self.export = QPushButton(self) self.export.setText('Export to file') self.export.clicked.connect(self.exportToFile) layout_bottom_options.addWidget(self.export) # Set up the toggle button to switch tree sorting modes self.switch_mode = QToolButton(self) self.switch_mode.setText('Sort tree by components') self.switch_mode.clicked.connect(self.switchMode) layout_left_options.addWidget(self.switch_mode) # Add instructions to sort the table self.how = QLabel(self) self.how.setText('Click each header to sort table') layout_left_options.addWidget(self.how) #################Set up the QTableView Widget############################# self.table = QTableView(self) self.table.setSortingEnabled(True) self.table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch) self.table.verticalHeader().setVisible(False) #Set the table headings self.headings = ('Subset', 'Dataset', 'Component', 'Mean', 'Median', 'Minimum', 'Maximum', 'Sum') self.data_frame = pd.DataFrame(columns=self.headings) self.data_accurate = pd.DataFrame(columns=self.headings) self.model = pandasModel(self.data_frame, self.dc) self.table.setModel(self.model) layout_table = QHBoxLayout() layout_table.addWidget(self.table) layout_table.stretch(10) # Finish nesting all the layouts main_layout = QVBoxLayout() main_layout.addWidget(self.treeview) main_layout.addLayout(layout_left_options) main_layout.addLayout(layout_table) main_layout.addLayout(layout_bottom_options) self.setLayout(main_layout) # Set up dict for caching self.cache_stash = dict()
def now(self): self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface) videoWidget = QVideoWidget() #self.videoWidget.setFullscreen(False) self.playButton = QPushButton() self.playButton.setEnabled(False) self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay)) self.playButton.clicked.connect(self.play) self.positionSlider = QSlider(Qt.Horizontal) self.positionSlider.setRange(0, 0) self.positionSlider.sliderMoved.connect(self.setPosition) self.errorLabel = QLabel() self.errorLabel.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum) # Create new action openAction = QPushButton(QIcon('open.png'), '&Open', self) openAction.setShortcut('Ctrl+O') openAction.setStatusTip('Open movie') openAction.clicked.connect(self.openFile) self.setStyleSheet("QPushButton { margin: 150px; }") # Create a widget for window contents wid = QWidget(self) #self.setCentralWidget(wid) # Create layouts to place inside widget frm = QFormLayout() #frm.setContentsMargins(50, 300,100,10) frm.addRow(openAction, self.playButton) #50, 100,100,10 mylay = QBoxLayout(QBoxLayout.TopToBottom) mylay.stretch(1) #videoWidget.setFullscreen(False) mylay.addWidget(videoWidget) mylay.addLayout(frm) mylay.addWidget(self.positionSlider) controlLayout = QHBoxLayout() controlLayout.stretch(1) controlLayout.setContentsMargins(0, 0, 0, 0) controlLayout.addLayout(mylay) layout = QVBoxLayout() layout.stretch(1) layout.addWidget(videoWidget) layout.addWidget(self.errorLabel) layout.addLayout(controlLayout) # Set widget to contain window contents wid.setLayout(layout) self.mediaPlayer.setVideoOutput(videoWidget) self.mediaPlayer.stateChanged.connect(self.mediaStateChanged) self.mediaPlayer.positionChanged.connect(self.positionChanged) self.mediaPlayer.durationChanged.connect(self.durationChanged) self.mediaPlayer.error.connect(self.handleError) self.tabk.setLayout(layout)