Skip to content

Commit 3eae729

Browse files
committed
add p34 QComboBox
1 parent b45b43c commit 3eae729

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# PyQt5基础教程(持续更新中。。。)
22

3+
* p34_QComboBox [源码下载](p34_QComboBox) [博文地址](https://xugaoxiang.com/2022/10/20/pyqt5-34-qcombobox/)
4+
35
* p33_窗口大小设置 [源码下载](p33_窗口大小设置) [博文地址](https://xugaoxiang.com/2022/10/06/pyqt5-33-window-size-setting/)
46

57
* p32_主题美化之`qt-material` [源码下载](p32_主题美化qtmaterial) [博文地址](https://xugaoxiang.com/2022/09/23/pyqt5-32-qt-material/)

p34_QComboBox/main.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import sys
2+
3+
from PyQt5.QtWidgets import QMainWindow, QApplication
4+
5+
from ui import Ui_MainWindow
6+
7+
8+
class MainWindow(QMainWindow, Ui_MainWindow):
9+
10+
def __init__(self, parent=None):
11+
super(MainWindow, self).__init__(parent)
12+
self.setupUi(self)
13+
14+
# 增加单个选项
15+
self.comboBox.addItem("Python")
16+
self.comboBox.addItem("Java")
17+
18+
# 增加多个选项
19+
self.comboBox.addItems(["C", "C++", "Javascript"])
20+
21+
# 绑定信号和槽
22+
# 当下拉选项的index发生变化,才发射这个信号,比如连续选中同一个选项,这个信号就不会发射
23+
self.comboBox.currentIndexChanged.connect(self.on_item_selected)
24+
25+
# 和currentIndexChanged不同的是,即使前后选择的是同一个选项,信号也会被发射
26+
self.comboBox.activated.connect(self.on_activated)
27+
28+
# 当焦点在下拉选项上时,信号被发射,此时的index是已经被选中的选项的index,默认是0
29+
self.comboBox.highlighted.connect(self.on_highlighted)
30+
31+
def on_item_selected(self):
32+
print("{}: {}, index={}, total count={}".format(self.label.text(), self.comboBox.currentText(), self.comboBox.currentIndex(), self.comboBox.count()))
33+
34+
def on_activated(self):
35+
print('on_activated.')
36+
37+
def on_highlighted(self):
38+
print('on_highlighted, index={}'.format(self.comboBox.currentIndex()))
39+
40+
if __name__ == '__main__':
41+
app = QApplication(sys.argv)
42+
windows = MainWindow()
43+
windows.show()
44+
sys.exit(app.exec_())

p34_QComboBox/project.ui

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>768</width>
10+
<height>579</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>QComboBox示例</string>
15+
</property>
16+
<widget class="QWidget" name="centralwidget">
17+
<widget class="QComboBox" name="comboBox">
18+
<property name="geometry">
19+
<rect>
20+
<x>220</x>
21+
<y>170</y>
22+
<width>271</width>
23+
<height>61</height>
24+
</rect>
25+
</property>
26+
<property name="font">
27+
<font>
28+
<pointsize>14</pointsize>
29+
</font>
30+
</property>
31+
</widget>
32+
<widget class="QLabel" name="label">
33+
<property name="geometry">
34+
<rect>
35+
<x>230</x>
36+
<y>90</y>
37+
<width>251</width>
38+
<height>51</height>
39+
</rect>
40+
</property>
41+
<property name="font">
42+
<font>
43+
<pointsize>18</pointsize>
44+
</font>
45+
</property>
46+
<property name="text">
47+
<string>你最喜欢的编程语言是</string>
48+
</property>
49+
</widget>
50+
</widget>
51+
<widget class="QMenuBar" name="menubar">
52+
<property name="geometry">
53+
<rect>
54+
<x>0</x>
55+
<y>0</y>
56+
<width>768</width>
57+
<height>23</height>
58+
</rect>
59+
</property>
60+
</widget>
61+
<widget class="QStatusBar" name="statusbar"/>
62+
</widget>
63+
<resources/>
64+
<connections/>
65+
</ui>

p34_QComboBox/ui.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Form implementation generated from reading ui file '.\project.ui'
4+
#
5+
# Created by: PyQt5 UI code generator 5.15.4
6+
#
7+
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
8+
# run again. Do not edit this file unless you know what you are doing.
9+
10+
11+
from PyQt5 import QtCore, QtGui, QtWidgets
12+
13+
14+
class Ui_MainWindow(object):
15+
def setupUi(self, MainWindow):
16+
MainWindow.setObjectName("MainWindow")
17+
MainWindow.resize(768, 579)
18+
self.centralwidget = QtWidgets.QWidget(MainWindow)
19+
self.centralwidget.setObjectName("centralwidget")
20+
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
21+
self.comboBox.setGeometry(QtCore.QRect(220, 170, 271, 61))
22+
font = QtGui.QFont()
23+
font.setPointSize(14)
24+
self.comboBox.setFont(font)
25+
self.comboBox.setObjectName("comboBox")
26+
self.label = QtWidgets.QLabel(self.centralwidget)
27+
self.label.setGeometry(QtCore.QRect(230, 90, 251, 51))
28+
font = QtGui.QFont()
29+
font.setPointSize(18)
30+
self.label.setFont(font)
31+
self.label.setObjectName("label")
32+
MainWindow.setCentralWidget(self.centralwidget)
33+
self.menubar = QtWidgets.QMenuBar(MainWindow)
34+
self.menubar.setGeometry(QtCore.QRect(0, 0, 768, 23))
35+
self.menubar.setObjectName("menubar")
36+
MainWindow.setMenuBar(self.menubar)
37+
self.statusbar = QtWidgets.QStatusBar(MainWindow)
38+
self.statusbar.setObjectName("statusbar")
39+
MainWindow.setStatusBar(self.statusbar)
40+
41+
self.retranslateUi(MainWindow)
42+
QtCore.QMetaObject.connectSlotsByName(MainWindow)
43+
44+
def retranslateUi(self, MainWindow):
45+
_translate = QtCore.QCoreApplication.translate
46+
MainWindow.setWindowTitle(_translate("MainWindow", "QComboBox示例"))
47+
self.label.setText(_translate("MainWindow", "你最喜欢的编程语言是"))

0 commit comments

Comments
 (0)