def initUI(self): self.setGeometry(self.xPos, self.yPos, self.width, self.height) self.vBoxLayout = QVBoxLayout() self.button = Button( borderWidth = 1, borderRadius = 4, ) self.button.setText('Default Button') self.vBoxLayout.addWidget(self.button) self.blueButton = Button( primaryColor = (17, 46, 133), secondaryColor = (202, 209, 232), borderWidth = 1, borderRadius = 4, ) self.blueButton.setText('Blue Button') self.vBoxLayout.addWidget(self.blueButton) self.redButton = Button( primaryColor = (171, 3, 3), secondaryColor = (247, 173, 173), borderWidth = 1, borderRadius = 4, ) self.redButton.setText('Red Button') self.vBoxLayout.addWidget(self.redButton) self.centralWidget = QWidget(self) self.centralWidget.setLayout(self.vBoxLayout) self.setCentralWidget(self.centralWidget) self.show()
def __init__(self, parent=None): QWidget.__init__(self, parent) self.l = l = QFormLayout(self) self.username = u = QLineEdit(self) u.textChanged.connect(self.changed.emit) l.addRow(_('&Username:'******'Username for this account')) self.password = p = QLineEdit(self) l.addRow(_('&Password:'******'Password for this account')) p.textChanged.connect(self.changed.emit) p.setEchoMode(QLineEdit.EchoMode.Password) self.show_password = sp = QCheckBox(_('&Show password')) l.addWidget(sp) sp.toggled.connect(self.show_password_toggled) self.la = la = QLabel(_('&Notes:')) l.addRow(la) self.notes = n = QPlainTextEdit(self) la.setBuddy(n) n.textChanged.connect(self.changed.emit) l.addRow(n) self.autosubmit = asb = QCheckBox( _('&Auto login with these credentials'), self) l.addRow(asb) asb.stateChanged.connect(self.on_change) self.rb = b = QPushButton(_('&Delete this account')) b.clicked.connect(self.delete_requested.emit) l.addRow(b)
def App(): app = QApplication(sys.argv) win = QWidget() win.setWindowTitle("PyQt6 QLabel Example") win.left = 100 win.top = 100 l1 = QLabel("Hello World") l2 = QLabel("Welcome to Python GUI Programming") # # Because you can't instantiate a QLable directly with a QPixmap. # l3 = QLabel() l3.setPixmap(QPixmap("python-small.png")) l1.setAlignment(Qt.Alignment.AlignCenter) l2.setAlignment(Qt.Alignment.AlignCenter) l3.setAlignment(Qt.Alignment.AlignCenter) vbox = QVBoxLayout() vbox.addWidget(l1) vbox.addStretch() vbox.addWidget(l2) vbox.addStretch() vbox.addWidget(l3) vbox.addStretch() win.setLayout(vbox) win.show() sys.exit(app.exec())
class Window(QMainWindow): def __init__(self): super().__init__() self.width = 150 self.height = 150 self.xPos = 600 self.yPos = 400 self.initUI() def initUI(self): self.setGeometry(self.xPos, self.yPos, self.width, self.height) self.vBoxLayout = QVBoxLayout() self.canvas = Canvas(width=150, height=150, penColor=(21, 21, 21), canvasColor=(245, 177, 66), strokeWidth=5, borderWidth=2, borderColor=(21, 21, 21)) self.vBoxLayout.addWidget(self.canvas, alignment=Qt.AlignmentFlag.AlignCenter) self.centralWidget = QWidget(self) self.centralWidget.setLayout(self.vBoxLayout) self.setCentralWidget(self.centralWidget) self.show()
def paintEvent(self, ev): p = QPainter(self) c = color('tab tree background', None) if c: p.fillRect(ev.rect(), QColor(c)) p.end() QWidget.paintEvent(self, ev)
def initUI(self): self.setGeometry(self.xPos, self.yPos, self.width, self.height) self.vBoxLayout = QVBoxLayout() self.tagbox = TagBox() self.tagbox.addTag('Homelander') self.tagbox.addTag('Queen Maeve') self.tagbox.addTag('Black Noir') self.tagbox.addTag('Transluscent') self.tagbox.addTag('A-Train') self.tagbox.addTag('The Deep') self.vBoxLayout.addWidget(self.tagbox) self.tagEdit = QLineEdit() self.vBoxLayout.addWidget(self.tagEdit) self.addButton = QPushButton() self.addButton.setText('Add New Tag') self.addButton.clicked.connect(self.addNewTag) self.vBoxLayout.addWidget(self.addButton) self.centralWidget = QWidget(self) self.centralWidget.setLayout(self.vBoxLayout) self.setCentralWidget(self.centralWidget) self.show()
def resizeEvent(self, event): # Fit content to view rect just when buffer fit_to_view option is enable. if self.buffer.fit_to_view: if event.oldSize().isValid(): self.graphics_view.fitInView( self.graphics_view.scene().sceneRect(), Qt.AspectRatioMode.KeepAspectRatio) QWidget.resizeEvent(self, event)
def __init__(self, parent): QWidget.__init__(self, parent) self.is_enabled = False self.setFocusPolicy(Qt.FocusPolicy.NoFocus) self.setCursor(Qt.CursorShape.PointingHandCursor) self.update_state(False) self.setMinimumWidth(STATUS_BAR_HEIGHT - 4) self.setMinimumHeight(STATUS_BAR_HEIGHT - 4)
def __init__(self, *args): QWidget.__init__(self, *args) self.edit = None # This is used to update the width of the control. # It is the highest line that is currently visibile. # self.highest_line = 0
def __addInputAndRadio__(self, input, radio): hbox = QHBoxLayout() hbox.setContentsMargins(0, 0, 0, 0) hbox.addWidget(input) hbox.addWidget(radio) widget = QWidget(self) widget.setLayout(hbox) self.layout.addWidget(widget, self.row, 0, 1, -1) self.row += 1
def configure_gui(self): self.center = QWidget(self) self.layout = QHBoxLayout() self.center.setLayout(self.layout) self.setCentralWidget(self.center) self.layout.setContentsMargins(5, 0, 5, 0) self.layout.setSpacing(0)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.l = l = QHBoxLayout(self) l.setContentsMargins(0, 0, 0, 0) self.la = la = QLabel(self) l.addWidget(la) self.edit = Search(self) la.setBuddy(self.edit) l.addWidget(self.edit)
def update(self, *args): # Updates the number bar width to display the current line numbers. # # The + 15 adds a bit of whitespace to the right of the line number. # width = int(log10(self.edit.document().blockCount()) + 1) * self.fontMetrics().averageCharWidth() + 5 if self.width() != width: self.setFixedWidth(width) QWidget.update(self, *args)
def initUI(self): self.setGeometry(self.xPos, self.yPos, self.width, self.height) self.vBoxLayout = QVBoxLayout() self.slider = Slider( direction=Qt.Orientation.Horizontal, duration=750, animationType=QEasingCurve.Type.OutQuad, wrap=False, ) self.label1 = QLabel() self.label1.setText('First Slide') self.label1.setAlignment(Qt.AlignmentFlag.AlignCenter) self.label1.setStyleSheet( 'QLabel{background-color: rgb(245, 177, 66); color: rgb(21, 21, 21); font: 25pt;}' ) self.slider.addWidget(self.label1) self.label2 = QLabel() self.label2.setText('Second Slide') self.label2.setAlignment(Qt.AlignmentFlag.AlignCenter) self.label2.setStyleSheet( 'QLabel{background-color: rgb(21, 21, 21); color: rgb(245, 177, 66); font: 25pt;}' ) self.slider.addWidget(self.label2) self.label3 = QLabel() self.label3.setText('Third Slide') self.label3.setAlignment(Qt.AlignmentFlag.AlignCenter) self.label3.setStyleSheet( 'QLabel{background-color: rgb(93, 132, 48); color: rgb(245, 177, 66); font: 25pt;}' ) self.slider.addWidget(self.label3) self.buttonPrevious = QPushButton() self.buttonPrevious.setText('Previous Slide') self.buttonPrevious.clicked.connect(self.slider.slidePrevious) self.buttonNext = QPushButton() self.buttonNext.setText('Next Slide') self.buttonNext.clicked.connect(self.slider.slideNext) self.buttonLayout = QHBoxLayout() self.buttonLayout.addWidget(self.buttonPrevious) self.buttonLayout.addWidget(self.buttonNext) self.vBoxLayout.addWidget(self.slider) self.vBoxLayout.addLayout(self.buttonLayout) self.centralWidget = QWidget(self) self.centralWidget.setLayout(self.vBoxLayout) self.setCentralWidget(self.centralWidget) self.show()
def __init__(self, parent, sb_background): QWidget.__init__(self, parent) self.is_permanent = False self.is_address = False self.is_secure = False self.static_text = None self.current_key = None self.setFocusPolicy(Qt.FocusPolicy.NoFocus) self.sb_background = QColor( color(sb_background, self.palette().color(QPalette.ColorRole.Window)))
def paintEvent(self, ev): painter = QPainter(self) painter.setRenderHint(QPainter.RenderHint.Antialiasing, True) painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform, True) try: self.paint_background(painter) except Exception: pass finally: painter.end() QWidget.paintEvent(self, ev)
def __addInputAndSelect__(self, input, top): hbox = QHBoxLayout() hbox.setContentsMargins(0, 0, 0, 0) hbox.addWidget(input) browseButton = BrowseButton(self, input, top) browseButton.adjustSize() hbox.addWidget(browseButton) widget = QWidget(self) widget.setLayout(hbox) self.layout.addWidget(widget, self.row, 0, 1, -1) self.row += 1
def paintEvent(self, event): contents_y = self.edit.verticalScrollBar().value() page_bottom = contents_y + self.edit.viewport().height() font_metrics = self.fontMetrics() current_block = self.edit.document().findBlock(self.edit.textCursor().position()) painter = QPainter(self) line_count = 0 # Iterate over all text blocks in the document. # block = self.edit.document().begin() while block.isValid(): line_count += 1 # The top left position of the block in the document. # position = self.edit.document().documentLayout().blockBoundingRect(block).topLeft() # Check if the position of the block is out side of the visible # area. # if position.y() > page_bottom: break # We want the line number for the selected line to be bold. # bold = False if block == current_block: bold = True font = painter.font() font.setBold(True) painter.setFont(font) # Draw the line number right justified at the y position of the # line. 3 is a magic padding number. drawText(x, y, text). # painter.drawText(self.width() - len(str(line_count)) * font_metrics.averageCharWidth() - 5, round(position.y()) - contents_y + font_metrics.ascent(), str(line_count)) # Remove the bold style if it was set previously. # if bold: font = painter.font() font.setBold(False) painter.setFont(font) block = block.next() self.highest_line = line_count painter.end() QWidget.paintEvent(self, event)
def __init__(self, parent=None): QWidget.__init__(self, parent) self.setCursor(Qt.CursorShape.PointingHandCursor) self.setToolTip(self.no_downloads_tooltip) pal = (parent or QApplication.instance()).palette() self.dark = pal.color(QPalette.ColorRole.Text) self.light = pal.color(QPalette.ColorRole.Base) self.update() self.setMinimumWidth(STATUS_BAR_HEIGHT - 4) self.setMinimumHeight(STATUS_BAR_HEIGHT - 4) self.running = False
def main(): app = QApplication(sys.argv) w = QWidget() w.resize(250, 200) w.move(300, 300) w.setWindowTitle('Simple') w.show() sys.exit(app.exec())
def __init__(self, parent=None): """Initializer.""" super().__init__(parent) self.setWindowTitle("RP Contacts") self.resize(550, 250) self.centralWidget = QWidget() self.setCentralWidget(self.centralWidget) self.layout = QHBoxLayout() self.centralWidget.setLayout(self.layout) self.ContactsModel = ContactsModel() self.setupUI()
def setUpMainWindow(self): """Set up the GUI's main window.""" header_label = QLabel("List of Users") # Create model and table objects model = QStandardItemModel() model.setColumnCount(3) model.setHorizontalHeaderLabels(["Name", "Birthdate", "Actions"]) table_view = QTableView() table_view.setEditTriggers( QAbstractItemView.EditTrigger.NoEditTriggers) # NOTE: Uncomment for table cells to be unselectable #table_view.setSelectionMode(QAbstractItemView.SelectionMode.NoSelection) table_view.setModel(model) table_view.horizontalHeader().setSectionResizeMode( 0, QHeaderView.ResizeMode.Stretch) names_list = ["Willman, Joshua", "Davis, Scott", "Garcia, Sky"] # Add items to each row in the table by looping over # the names_list and adding the date edit and button widgets for row, name in enumerate(names_list): model.setItem(row, QStandardItem(name)) # Setting the widget at an index in a QTableView involves # acquiring the QModelIndex values of the current position. # One way to do this is to use the QAbstractItemModel.sibling() # method to retrieve the QModelIndex index from the specified # row and column (here the column is 1) index = table_view.model().sibling(row, 1, QModelIndex()) date_edit = QDateEdit(QDate.currentDate( )) # Create QDateEdit object that starts at current date date_edit.setDateRange(QDate(1900, 1, 1), QDate.currentDate()) date_edit.setDisplayFormat("MM/dd/yyyy") date_edit.setAlignment( Qt.AlignmentFlag.AlignRight) # Align the text date_edit.setAutoFillBackground(True) table_view.setIndexWidget(index, date_edit) # Set the widgets in the final column for each row index = table_view.model().sibling(row, 2, QModelIndex()) table_view.setIndexWidget(index, EditCellWidget(table_view)) # Set up main layout and container object for main window main_v_box = QVBoxLayout() main_v_box.addWidget(header_label) main_v_box.addWidget(table_view) container = QWidget() container.setLayout(main_v_box) self.setCentralWidget(container)
def execute(self): app = QApplication([]) tab1 = 20 tab2 = 110 yStart = 10 yInc = 20 w = QWidget() w.setGeometry(100, 100, 320, 240) w.setWindowTitle('World Clock') self._utc_label = self._create_label(w, 'UTC:', yStart, tab1, tab2) self._eastern_label = self._create_label(w, 'US/Eastern:', yStart + yInc * 2, tab1, tab2) self._central_label = self._create_label(w, 'US/Central:', yStart + yInc * 3, tab1, tab2) self._mountain_label = self._create_label(w, 'US/Mountain:', yStart + yInc * 4, tab1, tab2) self._pacific_label = self._create_label(w, 'US/Pacific:', yStart + yInc * 5, tab1, tab2) self._berlin_label = self._create_label(w, 'Berlin:', yStart + yInc * 7, tab1, tab2) self._london_label = self._create_label(w, 'London:', yStart + yInc * 8, tab1, tab2) self._paris_label = self._create_label(w, 'Paris:', yStart + yInc * 9, tab1, tab2) w.show() self._timer.start(1000) exit(app.exec())
def __init__(self): """View initializer.""" super().__init__() # Set some main window's properties self.setWindowTitle("PyCalc") self.setFixedSize(235, 235) # Set the central widget and the general layout self.generalLayout = QVBoxLayout() self._centralWidget = QWidget(self) self.setCentralWidget(self._centralWidget) self._centralWidget.setLayout(self.generalLayout) # Create the display and the buttons self._createDisplay() self._createButtons()
def keyPressEvent(self, ev): k = ev.key() if k == Qt.Key.Key_Escape: c = self.callback if c is not None: c(None) self.close() if self.parent() is None else self.hide() ev.accept() return if k == Qt.Key.Key_Tab: self.next_completion() ev.accept() return if k == Qt.Key.Key_Backtab: self.next_completion(forward=False) ev.accept() return if k in (Qt.Key.Key_Enter, Qt.Key.Key_Return): c = self.callback self.close() if self.parent() is None else self.hide() if c is not None: c(self.edit.text()) else: self.run_command.emit(self.edit.text()) return QWidget.keyPressEvent(self, ev)
def __init__(self, parent, item_type, floating_widget=None): super(CustomScrollableList, self).__init__() self.parent = parent self.item_type = item_type self.floating_widget = floating_widget self.layout = QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.list_widget = QWidget() self.list_layout = QVBoxLayout(self.list_widget) self.list_layout.setContentsMargins(0, 0, 0, 0) self.list_layout.setSpacing(10) self.list_layout.setAlignment(Qt.AlignmentFlag.AlignTop) self.scroll_area = QScrollArea() self.scroll_area.setWidgetResizable(True) self.scroll_area.setFrameStyle(0) self.scroll_area.setWidget(self.list_widget) self.layout.addWidget(self.scroll_area) if self.floating_widget is not None: self.list_layout.addWidget(self.floating_widget) self.item_widgets = [] self.num_visible_item_widgets = 0
def __init__(self, interactive_matching_widget): super(NuggetListWidget, self).__init__(interactive_matching_widget) self.interactive_matching_widget = interactive_matching_widget self.layout = QVBoxLayout(self) self.layout.setContentsMargins(0, 0, 0, 0) self.layout.setSpacing(10) # top widget self.top_widget = QWidget() self.top_layout = QHBoxLayout(self.top_widget) self.top_layout.setContentsMargins(0, 0, 0, 0) self.top_layout.setSpacing(10) self.layout.addWidget(self.top_widget) self.description = QLabel( "Below you see a list of guessed matches for you to confirm or correct." ) self.description.setFont(LABEL_FONT) self.top_layout.addWidget(self.description) self.stop_button = QPushButton("Continue With Next Attribute") self.stop_button.setFont(BUTTON_FONT) self.stop_button.clicked.connect(self._stop_button_clicked) self.stop_button.setMaximumWidth(240) self.top_layout.addWidget(self.stop_button) # nugget list self.nugget_list = CustomScrollableList(self, NuggetListItemWidget) self.layout.addWidget(self.nugget_list)
def add_action(self, name: str, func: Callable, menu: str = None, shortcut: str = None, tooltip: str = None, label_text: str = None, parent: QWidget = None, **kw) -> QAction: """Convenience func to create QAction and add to menu bar Parameters ---------- name : str Action name Returns ------- QAction """ name_action = name.replace(' ', '_').lower() name_key = f'act_{name_action}' name = f.nice_title(name.replace( '_', ' ')) if label_text is None else label_text if parent is None: parent = self act = QAction(name, parent, triggered=func, **kw) if not shortcut is None: act.setShortcut(QKeySequence(shortcut)) act.setToolTip(tooltip) # act.setShortcutContext(Qt.ShortcutContext.WidgetShortcut) act.setShortcutVisibleInContextMenu(True) setattr(parent, name_key, act) if not menu is None: menu = self.get_menu(menu) menu.addAction(act) else: parent.addAction(act) return act
def __init__(self): app = QApplication(sys.argv) super().__init__() self.setWindowTitle('Generic Configuration Tool') self.setGeometry(100, 100, 640, 480) self.isEdited = False self.saveAndClose = SaveAndCloseActions(self) centralWidget = QWidget() vbox = QVBoxLayout() centralWidget.setLayout(vbox) vbox.addWidget(TabContainer(self)) vbox.addStretch(1) vbox.addWidget(self.saveAndClose) self.setCentralWidget(centralWidget) self.show() sys.exit(app.exec())
def setUpMainWindow(self): """Set up the GUI's main window.""" header_label = QLabel("List of Users") # Create model and table objects model = QStandardItemModel() model.setColumnCount(3) model.setHorizontalHeaderLabels(["Name", "Birthdate", "Actions"]) table_view = QTableView() # NOTE: setEditTriggers() is not used so that the user # can double-click and edit cells table_view.setModel(model) table_view.horizontalHeader().setSectionResizeMode( 0, QHeaderView.ResizeMode.Stretch) # Set the item delegate for a specific column, in this case column 1 table_view.setItemDelegateForColumn(1, DateEditDelegate()) names_list = ["Willman, Joshua", "Davis, Scott", "Garcia, Sky"] # Add items to each row in the table by looping over # the names_list and adding the date edit and button widgets for row, name in enumerate(names_list): model.setItem(row, QStandardItem(name)) # Create an item and set the initial value for the second column. # Here the QDate values are converted to strings to make it easier # to align the text without having to subclass a model class date_item = QStandardItem( QDate.currentDate().toString("MM/dd/yyyy")) date_item.setTextAlignment(Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight) model.setItem(row, 1, date_item) # Set the widgets in the final column for each row index = model.index(row, 2, QModelIndex()) table_view.setIndexWidget(index, EditCellWidget(table_view)) # Set up main layout and container object for main window main_v_box = QVBoxLayout() main_v_box.addWidget(header_label) main_v_box.addWidget(table_view) container = QWidget() container.setLayout(main_v_box) self.setCentralWidget(container)