def __init__(self): super().__init__() widget = QSpinBox() # QDoubleSpinBox() widget.setRange(-10, 10) widget.setPrefix("$") widget.setSuffix("c") widget.setSingleStep(1) # 0.1 widget.valueChanged.connect(self.value_changed) widget.valueChanged[str].connect(self.value_changed_str) self.setCentralWidget(widget)
class NetPositionUI(QWidget): def __init__(self, *args, **kwargs): super(NetPositionUI, self).__init__(*args, **kwargs) main_layout = QVBoxLayout() main_layout.setContentsMargins(QMargins(2, 1, 2, 1)) main_layout.setSpacing(1) # 操作栏 opt_layout = QHBoxLayout() self.interval_days = QSpinBox(self) self.interval_days.setMinimum(1) self.interval_days.setMaximum(30) self.interval_days.setValue(5) self.interval_days.setPrefix("日期间隔 ") self.interval_days.setSuffix(" 天") opt_layout.addWidget(self.interval_days) self.query_button = QPushButton('确定', self) opt_layout.addWidget(self.query_button) self.tip_label = QLabel('左侧可选择间隔天数,确定查询数据. ', self) opt_layout.addWidget(self.tip_label) opt_layout.addStretch() main_layout.addLayout(opt_layout) # 显示数据的表 self.data_table = QTableWidget(self) self.data_table.setFrameShape(QFrame.NoFrame) self.data_table.setEditTriggers(QAbstractItemView.NoEditTriggers) # 不可编辑 self.data_table.setFocusPolicy(Qt.NoFocus) # 去选中时的虚线框 self.data_table.setAlternatingRowColors(True) # 交替行颜色 self.data_table.horizontalHeader().setDefaultSectionSize(85) # 默认的标题头宽 self.data_table.verticalHeader().hide() self.data_table.verticalHeader().setDefaultSectionSize(18) # 设置行高(与下行代码同时才生效) self.data_table.verticalHeader().setMinimumSectionSize(18) main_layout.addWidget(self.data_table) self.setLayout(main_layout) self.tip_label.setObjectName("tipLabel") self.data_table.setObjectName("dataTable") self.data_table.horizontalHeader().setStyleSheet("QHeaderView::section," "QTableCornerButton::section{height:25px;background-color:rgb(243,245,248);font-weight:bold;font-size:13px}") self.setStyleSheet( "#tipLabel{color:rgb(230,50,50);font-weight:bold;}" "#dataTable::item{padding:2px}" "#dataTable{selection-color:rgb(255,255,255);selection-background-color:rgb(51,143,255);alternate-background-color:rgb(245,250,248)}" )
def __init__(self): super().__init__() self.setWindowTitle("Line edit") widget = QSpinBox() # QDoubleSpinBox is for floats widget.setMinimum(-10) widget.setMaximum(3) # or: widget.setRange(-10, 3) widget.setPrefix("$") widget.setSuffix("c") widget.setSingleStep(3) widget.valueChanged.connect(self.value_changed) widget.valueChanged[str].connect(self.value_changed_str) self.setCentralWidget(widget)
def __init__(self): super().__init__() self.setWindowTitle("My App") widget = QSpinBox() # Or: widget = QDoubleSpinBox() widget.setMinimum(-10) widget.setMaximum(3) # Or: widget.setRange(-10,3) widget.setPrefix("$") widget.setSuffix("c") widget.setSingleStep(3) # Or e.g. 0.5 for QDoubleSpinBox widget.valueChanged.connect(self.value_changed) widget.textChanged.connect(self.value_changed_str) self.setCentralWidget(widget)
class PlanesWidget(ToolWidget): def __init__(self, image, parent=None): super(PlanesWidget, self).__init__(parent) self.chan_combo = QComboBox() self.chan_combo.addItems( [self.tr('Luminance'), self.tr('Red'), self.tr('Green'), self.tr('Blue'), self.tr('RGB Norm')]) self.plane_spin = QSpinBox() self.plane_spin.setPrefix(self.tr('Bit ')) self.plane_spin.setRange(0, 7) self.filter_combo = QComboBox() self.filter_combo.addItems([self.tr('Disabled'), self.tr('Median'), self.tr('Gaussian')]) self.image = image self.viewer = ImageViewer(self.image, self.image) self.planes = None self.preprocess() self.chan_combo.currentIndexChanged.connect(self.preprocess) self.plane_spin.valueChanged.connect(self.process) self.filter_combo.currentIndexChanged.connect(self.process) top_layout = QHBoxLayout() top_layout.addWidget(QLabel(self.tr('Channel:'))) top_layout.addWidget(self.chan_combo) top_layout.addWidget(QLabel(self.tr('Plane:'))) top_layout.addWidget(self.plane_spin) top_layout.addWidget(QLabel(self.tr('Filter:'))) top_layout.addWidget(self.filter_combo) top_layout.addStretch() main_layout = QVBoxLayout() main_layout.addLayout(top_layout) main_layout.addWidget(self.viewer) self.setLayout(main_layout) def preprocess(self): channel = self.chan_combo.currentIndex() if channel == 0: img = cv.cvtColor(self.image, cv.COLOR_BGR2GRAY) elif channel == 4: b, g, r = cv.split(self.image.astype(np.float64)) img = cv.sqrt(cv.pow(b, 2) + cv.pow(g, 2) + cv.pow(r, 2)).astype(np.uint8) else: img = self.image[:, :, 3 - channel] self.planes = [normalize_mat(cv.bitwise_and(np.full_like(img, 2**b), img), to_bgr=True) for b in range(8)] # rows, cols = img.shape # bits = 8 # data = [np.binary_repr(img[i][j], width=bits) for i in range(rows) for j in range(cols)] # self.planes = [ # (np.array([int(i[b]) for i in data], dtype=np.uint8) * 2 ** (bits - b - 1)).reshape( # (rows, cols)) for b in range(bits)] self.process() def process(self): plane = self.planes[self.plane_spin.value()] if self.filter_combo.currentIndex() == 1: plane = cv.medianBlur(plane, 3) elif self.filter_combo.currentIndex() == 2: plane = cv.GaussianBlur(plane, (3, 3), 0) self.viewer.update_processed(plane)
class EmptyVolumeUI(QSplitter): def __init__(self, *args, **kwargs): super(EmptyVolumeUI, self).__init__(*args, **kwargs) self.visible = QGraphicsOpacityEffect(self) self.visible.setOpacity(1.0) self.disvisible = QGraphicsOpacityEffect(self) self.disvisible.setOpacity(0.0) main_layout = QHBoxLayout() self.variety_tree = VarietyTree(self) main_layout.addWidget(self.variety_tree) self.right_widget = QWidget(self) right_layout = QVBoxLayout() right_layout.setContentsMargins(QMargins(1, 1, 1, 1)) opts_layout = QHBoxLayout() # 选择分析的目标数据类别 self.radio_button_group = QButtonGroup(self) radio_button_1 = QRadioButton("行情统计", self) radio_button_1.setChecked(True) self.radio_button_group.addButton(radio_button_1) radio_button_2 = QRadioButton("排名持仓", self) self.radio_button_group.addButton(radio_button_2) opts_layout.addWidget(radio_button_1) opts_layout.addWidget(radio_button_2) self.rank_spinbox = QSpinBox(self) self.rank_spinbox.setPrefix("前 ") self.rank_spinbox.setSuffix(" 名") self.rank_spinbox.setRange(1, 20) self.rank_spinbox.setValue(20) self.rank_spinbox.setEnabled(False) opts_layout.addWidget(self.rank_spinbox) # 分割线 vertical_line = QFrame(self) vertical_line.setFrameShape(QFrame.VLine) opts_layout.addWidget(vertical_line) opts_layout.addWidget(QLabel("选择合约:", self)) self.contract_combobox = QComboBox(self) opts_layout.addWidget(self.contract_combobox) self.confirm_button = QPushButton("确定", self) opts_layout.addWidget(self.confirm_button) self.tip_button = QPushButton('左侧选择品种后进行查询 ', self) # 提示文字 opts_layout.addWidget(self.tip_button) self.tip_button.setGraphicsEffect(self.disvisible) opts_layout.addStretch() right_layout.addLayout(opts_layout) self.web_container = QWebEngineView(self) right_layout.addWidget(self.web_container) self.right_widget.setLayout(right_layout) main_layout.addWidget(self.right_widget) self.setStretchFactor(1, 2) self.setStretchFactor(2, 8) self.setHandleWidth(1) self.contract_combobox.setMinimumWidth(80) self.setLayout(main_layout) self.tip_button.setObjectName("tipButton") self.setStyleSheet( "#tipButton{border:none;color:rgb(230,50,50);font-weight:bold;}")
class Formulario(QWidget): def __init__(self): super().__init__() self.iniciaUI() def iniciaUI(self): """ Inicializa a janela e mostra seu conteuda na tela """ self.setGeometry(100, 100, 300, 200) self.setWindowTitle("Formulario") self.displayWidgets() self.show() def displayWidgets(self): """ Configura os widgets da app """ info_lbl = QLabel("Selecione 2 itens que você almoçou e seus preços.") info_lbl.setFont(QFont('Arial', 16)) info_lbl.setAlignment(Qt.AlignCenter) self.total_lbl = QLabel("Total: R$") self.total_lbl.setFont(QFont('Arial', 16)) self.total_lbl.setAlignment(Qt.AlignRight) list_comida = [ "ovos", "misto quente", "queijo quente", "queijo", "homus", "iogurte", "maçã", "banana", "laranja", "pão de queijo", "cenouras", "pão", "macarrão", "biscoitos", "tapioca", "batatas fritas", "café", "refrigerante", "água" ] alm1_cbx = QComboBox() alm1_cbx.addItems(list_comida) alm2_cbx = QComboBox() alm2_cbx.addItems(list_comida) self.pre1R_sbx = QSpinBox() self.pre1R_sbx.setRange(0, 100) self.pre1R_sbx.setPrefix("R$ ") self.pre1R_sbx.valueChanged.connect(self.calculaTotal) self.pre1C_sbx = QSpinBox() self.pre1C_sbx.setRange(0, 99) self.pre1C_sbx.setPrefix(".") self.pre1C_sbx.valueChanged.connect(self.calculaTotal) self.pre2R_sbx = QSpinBox() self.pre2R_sbx.setRange(0, 100) self.pre2R_sbx.setPrefix("R$ ") self.pre2R_sbx.valueChanged.connect(self.calculaTotal) self.pre2C_sbx = QSpinBox() self.pre2C_sbx.setRange(0, 99) self.pre2C_sbx.setPrefix(".") self.pre2C_sbx.valueChanged.connect(self.calculaTotal) hbox1 = QHBoxLayout() hbox2 = QHBoxLayout() hbox1.addWidget(alm1_cbx) hbox1.addWidget(self.pre1R_sbx) hbox1.addWidget(self.pre1C_sbx) hbox2.addWidget(alm2_cbx) hbox2.addWidget(self.pre2R_sbx) hbox2.addWidget(self.pre2C_sbx) vbox = QVBoxLayout() vbox.addWidget(info_lbl) vbox.addLayout(hbox1) vbox.addLayout(hbox2) vbox.addWidget(self.total_lbl) self.setLayout(vbox) def calculaTotal(self): """ Calcular e exibir o preço total das spin boxes e alterar o valor mostrado no QLabel """ total = self.pre1R_sbx.value() + self.pre2R_sbx.value() total += (self.pre1C_sbx.value() / 100) total += (self.pre2C_sbx.value() / 100) self.total_lbl.setText("Total: R${}".format(str(total)))
class Bucket(QWidget): IMG_EMPTY = QIcon("img/Empty_bucket.svg") IMG_FULL = QIcon("img/Full_bucket.svg") IMG_HALF = QIcon("img/Half_full_bucket.svg") selected = Signal(object) removed = Signal(object) def __init__(self, capacity, init, goal): super().__init__() self.capacity = capacity self.init = init self.current = init self.goal = goal self.editable = False self.btn = QPushButton() self.btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.btn.clicked.connect(self.select) self.layout = QVBoxLayout() self.layout.addWidget(self.btn) self.btn_remove = QPushButton("X") self.btn_remove.clicked.connect(self.remove) self.layout.addWidget(self.btn_remove) self.edit_capacity = QSpinBox() self.edit_capacity.setPrefix("Capacity ") self.edit_capacity.setSuffix(" L") self.edit_capacity.valueChanged.connect(self.change_capacity) self.layout.addWidget(self.edit_capacity) self.edit_capacity.hide() self.edit_init = QSpinBox() self.edit_init.setPrefix("Init ") self.edit_init.setSuffix(" L") self.edit_init.valueChanged.connect(self.change_init) self.layout.addWidget(self.edit_init) self.edit_init.hide() self.edit_goal = QSpinBox() self.edit_goal.setPrefix("Goal ") self.edit_goal.setSuffix(" L") self.edit_goal.valueChanged.connect(self.change_goal) self.layout.addWidget(self.edit_goal) self.edit_goal.hide() self.update() self.setLayout(self.layout) def switch_editable(self): self.editable = not self.editable if self.editable: self.edit_capacity.show() self.edit_init.show() self.edit_goal.show() self.btn.setEnabled(False) else: self.edit_capacity.hide() self.edit_init.hide() self.edit_goal.hide() self.btn.setEnabled(True) def change_capacity(self, capacity): self.capacity = capacity self.update() def change_init(self, init): self.init = init self.current = init self.update() def change_goal(self, goal): self.goal = goal self.update() def reset(self): self.current = self.init self.update() def remove(self): self.removed.emit(self) def select(self): self.selected.emit(self) def update(self): self.update_spin() self.update_image() self.update_text() def update_spin(self): self.edit_capacity.setValue(self.capacity) self.edit_capacity.setMinimum(self.init) self.edit_init.setValue(self.init) self.edit_init.setMaximum(self.capacity) self.edit_goal.setValue(self.goal) self.edit_goal.setMaximum(self.capacity) def update_text(self): self.btn.setText(f"{self.current} ({self.goal}) / {self.capacity} L") def update_image(self): if self.current == 0: self.btn.setIcon(self.IMG_EMPTY) elif self.current == self.capacity: self.btn.setIcon(self.IMG_FULL) else: self.btn.setIcon(self.IMG_HALF) def emptying(self, bucket): value = min([self.current, bucket.capacity - bucket.current]) self.current -= value self.update() bucket.current += value bucket.update() def full(self): return self.capacity == self.current def good(self): return self.goal == self.current def changed_color(self, actived=False, color=Qt.blue): pal = QPushButton().palette() # self.btn if actived: pal.setColor(QPalette.Button, QColor(color)) elif self.good(): pal.setColor(QPalette.Button, QColor(Qt.green)) self.btn.setPalette(pal)