气轻PyQt6 20 下拉菜单(QComboBox)

2023-04-10 08:00:38    来源:哔哩哔哩

在下拉菜单选中的选项会显示在标签中。

from PyQt6.QtWidgets import *


(资料图片)

from PyQt6.QtCore import Qt

import sys

timeLabel = ['早饭前', '早饭后', '午饭前', '午饭后', '晚饭前', '晚饭后']

class PyQt620(QWidget):

def __init__(self):

super().__init__()

self.initUI()

def initUI(self):

self.setWindowTitle('气轻PyQt6')                # 设置窗口标题

self.resize(420, 50)                           # 设置窗口大小

self.setStyleSheet('background-color:#FAEBD7')

self.stage = QLabel(self)                       #设置label信息

self.stage.setGeometry(10, 0,230, 40)           # 设置位置和大小

self.stage.setText(timeLabel[0])

self.stage.setObjectName('label')

self.stage.setToolTip('时段')                   # 设置label提示

self.stage.setAlignment(Qt.AlignmentFlag.AlignHCenter)# 居中设置

self.stage.setStyleSheet('background-color:#ADD8E6;color : #00BFFF; \

font: bold italic large /"Times New Roman/";font-size:25px')

self.combobox = QComboBox(self, minimumWidth=100)

self.combobox.setGeometry(240, 0,170, 40)          # 设置位置和大小

for i in range(len(timeLabel)):

self.combobox.addItem(timeLabel[i])

self.combobox.setCurrentIndex(0)

self.combobox.setStyleSheet('background-color:#ADD8E6;color : #00BFFF; \

font: bold italic large /"Times New Roman/";font-size:25px')

self.combobox.currentIndexChanged.connect(self.updateStage)

self.show()

def updateStage(self,val):

self.stage.setText(self.combobox.currentText())

if __name__ == '__main__':

app = QApplication(sys.argv)

window = PyQt620()

sys.exit(app.exec())

执行结果

标签:

上一篇:

下一篇:

X 关闭

X 关闭