当前位置:网站首页>Pyqt control part (II)
Pyqt control part (II)
2022-07-03 04:29:00 【aijiayi00】
Catalog
result :
1. The text selected in the next combo box is label Real time text display
2. The font selected in the font combo box can be changed label The font of the text
3. Use the group box to group radio buttons
4. Real time addition and deletion of tabs ( Achieved with buttons )
5. The date and time control displays the local time ( But it won't change , It's not real time )
6. Calendar control selects a date , The date will be output on the console
This time I learned some new controls , It is estimated that we will start learning next time PyQt5 Advanced controls for .
The controls for this learning are :
ComboBox: Drop down combo box
FontComoBox: Font combo box
ListWidget: list
GroupBox: Group box
TabWidget: tab
Date and time controls :DateTimeEdit、DateEdit、TimeEdit
CalendarWidget: calendar control
Of course, for controls , Just put it there , It can't achieve any function .
Use controls to achieve certain functions , You need to know what signal this control will emit , And let the signal be bound to a method .
And we just need to put the logical operations and ideas we want to perform , Just write it in the method .
Some common signals emitted by the above controls :
ComboBox Control will emit currentIndexChanged( Emit when the drop-down index changes )
FontComboBox Controls and ComboBox be similar .TabWidget Control will emit currentChanged The signal ( When switching tabs , launch )
The date and time control will emit three signals :timeChanged、dateChanged、datetTimeChanged
Launch when time changes timeChanged The signal
Launch when the date changes dateChanged The signal
Launch when the time or date changes dateTimeChanged The signalCalendarWidget Control will emit selectionChanged The signal
Launch when the date of election changes selectionChanged The signal
This study has achieved
1. The text selected in the next combo box is label Real time text display ,
2. The font selected in the font combo box can be changed label The font of the text ,
3. Use the group box to group radio buttons .
4. Real time addition and deletion of tabs ( Achieved with buttons ),
5. The date and time control displays the local time ( But it won't change , It's not real time ),
6. Calendar control selects a date , The date will be output on the console .
The console is print Output place .
Code up :
label_2.py:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'label_2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Drop down combo box (ComboBox)
self.comboBox = QtWidgets.QComboBox(self.centralwidget)
self.comboBox.setGeometry(QtCore.QRect(10, 10, 80, 30))
self.comboBox.setObjectName("comboBox")
mid = ' nothing '
list = [' Basketball ', ' football ', ' Table Tennis ', ' badminton ']
self.comboBox.addItem(mid) # Add options to the combo box -- Add a single variable
self.comboBox.addItems(list) # Add options to the combo box -- Add all variables in the list
self.comboBox.currentIndexChanged.connect(self.showinfo_comboBox) # Change index , Send a signal , Execute the procedure in the method
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 40, 210, 30))
self.label.setObjectName("label")
self.label.setText(" Your favorite sport is :" + self.comboBox.currentText())
# Font combo box (FontComboBox)-- Mainly used to change the font of text
self.fontComboBox = QtWidgets.QFontComboBox(self.centralwidget)
self.fontComboBox.setGeometry(QtCore.QRect(10, 70, 100, 30))
self.fontComboBox.setObjectName("fontComboBox")
self.fontComboBox.setFontFilters(QtWidgets.QFontComboBox.AllFonts) # Show all fonts in the combo box
self.fontComboBox.currentIndexChanged.connect(self.showinfo_comboBox)
# list (ListWidget)
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setGeometry(QtCore.QRect(200, 0, 300, 200))
self.listWidget.setObjectName("listWidget")
self.listWidget.setViewMode(QtWidgets.QListView.ListMode) # Set to display data in a list
self.listWidget.setWordWrap(True) # Set to turn on auto wrap
self.listWidget.addItems(list)
dict = {'1':' Basketball ','2':' football ','3':' badminton ','4':' Table Tennis ','5':' rugby '} # Add key value pairs from the dictionary to the list
from collections import OrderedDict # The dictionary is unordered by default , We can import this module
dict = OrderedDict(dict) # Make the dictionary into an ordered dictionary
for key, value in dict.items():
self.item = QtWidgets.QListWidgetItem(self.listWidget) # Create a list item separately in the list ,
self.item.setText(key+':'+value) # So you can add values one by one
self.item.setToolTip(value) # Set prompts one by one
# Group box (GroupBox)-- Child window control
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(0, 100, 200, 100))
self.groupBox.setObjectName("groupBox")
self.groupBox.setTitle(' grouping 1')
self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox_2.setGeometry(QtCore.QRect(0, 200, 200, 100))
self.groupBox_2.setObjectName("groupBox_2")
self.groupBox_2.setTitle(' grouping 2')
# Radio button (RadioButton)-- Only one radio button in the same group can be selected
self.radioButton = QtWidgets.QRadioButton(self.groupBox) # If you want to put the control in the grouping box ,
self.radioButton.setGeometry(QtCore.QRect(0, 0, 80, 50)) # Directly set the parent window as a grouping box .
self.radioButton.setObjectName("radioButton")
self.radioButton.setText(" choice A")
self.radioButton_2 = QtWidgets.QRadioButton(self.groupBox)
self.radioButton_2.setGeometry(QtCore.QRect(80, 0, 80, 50))
self.radioButton_2.setObjectName("radioButton_2")
self.radioButton_2.setText(" choice B")
self.radioButton_3 = QtWidgets.QRadioButton(self.groupBox_2)
self.radioButton_3.setGeometry(QtCore.QRect(0, 0, 80, 50))
self.radioButton_3.setObjectName("radioButton_3")
self.radioButton_3.setText(" choice C")
self.radioButton_4 = QtWidgets.QRadioButton(self.groupBox_2)
self.radioButton_4.setGeometry(QtCore.QRect(80, 0, 80, 50))
self.radioButton_4.setObjectName("radioButton_4")
self.radioButton_4.setText(" choice D")
# tab (TabWidget)
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(0,400,400,150))
self.tabWidget.setTabsClosable(True)
# The set button is used to add an option window
self.pushBotton = QtWidgets.QPushButton(self.centralwidget)
self.pushBotton.setGeometry(QtCore.QRect(400, 400, 60, 30))
self.pushBotton.setObjectName("pushBotton")
self.pushBotton.setText(' add to ')
self.pushBotton.clicked.connect(self.addtab)
# The set button is used to delete a window
self.pushBotton_2 = QtWidgets.QPushButton(self.centralwidget)
self.pushBotton_2.setGeometry(QtCore.QRect(400, 460, 60, 30))
self.pushBotton_2.setObjectName("pushBotton_2")
self.pushBotton_2.setText(' Delete ')
self.pushBotton_2.clicked.connect(self.deltab)
# Date and time controls (DataTimeEdit)
self.dateTimeEdit = QtWidgets.QDateTimeEdit(self.centralwidget)
self.dateTimeEdit.setGeometry(QtCore.QRect(500, 200, 200, 30))
self.dateTimeEdit.setObjectName("dateTimeEdit")
self.dateTimeEdit.setDisplayFormat("yyyy-MM-dd HH:mm:ss") # Set the display form
self.dateTimeEdit.setCalendarPopup(True) # Setting can pop up the calendar
# self.dateTimeEdit.setTime(QtCore.QTime(1,59)) # Set the time to 1:59
self.dateTimeEdit.setDateTime(QtCore.QDateTime.currentDateTime()) # Set the display to get the current date and time
print(self.dateTimeEdit.text()) # String type
print(type(self.dateTimeEdit.text()))
print(self.dateTimeEdit.dateTime()) # QDateTime type
# calendar control (CalendarWidget)
self.calendarWidget = QtWidgets.QCalendarWidget(self.centralwidget)
self.calendarWidget.setGeometry(QtCore.QRect(480, 250, 310, 350))
self.calendarWidget.setObjectName("calendarWidget")
self.calendarWidget.setFirstDayOfWeek(QtCore.Qt.Monday) # Set the first day of the week as Monday
self.calendarWidget.setGridVisible(True) # Set gridlines visible
self.calendarWidget.setSelectionMode(QtWidgets.QCalendarWidget.SingleSelection) # You can select a date
self.calendarWidget.setDateEditEnabled(True) # Set date can be edited
self.calendarWidget.selectionChanged.connect(self.getdate) # When the date of election changes , launch selectionChange The signal
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def showinfo_comboBox(self):
# Change the font -- Font combo box
print(self.fontComboBox.currentText())
self.label.setFont(QtGui.QFont(self.fontComboBox.currentText()))
# According to the text -- Drop down the text in the combo box
self.label.setText(" Your favorite sport is :"+self.comboBox.currentText())
def addtab(self): # Add Tab
self.atab = QtWidgets.QWidget() # Create tab objects
name = 'tab_'+str(self.tabWidget.count())
self.atab.setObjectName(name) # Set its object name
self.tabWidget.addTab(self.atab, name)
def deltab(self): # Delete tab
self.tabWidget.removeTab(self.tabWidget.currentIndex()) # Remove the current tab
# self.tabWidget.currentIndex() ------ Get the index of the current tab -- from 0 Start adding one gradually
def getdate(self):
date = QtCore.QDate(self.calendarWidget.selectedDate()) # Get the... Of the currently selected date QDate object
# obtain Qdate Year of object 、 month 、 Japan .
year = date.year()
month = date.month()
day = date.day()
print(' date ', end=':')
print(year, month, day, sep='-')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
label_2_two.py
from label_2 import Ui_MainWindow
from PyQt5 import QtWidgets
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())result
Run the program :

1. The text selected in the next combo box is label Real time text display

2. The font selected in the font combo box can be changed label The font of the text


3. Use the group box to group radio buttons


4. Real time addition and deletion of tabs ( Achieved with buttons )

Click on ‘’ add to ‘’ Button

Click on “ Delete button ”

5. The date and time control displays the local time ( But it won't change , It's not real time )
![]()
6. Calendar control selects a date , The date will be output on the console


This is the result .
Screenshot words , I don't know why I can't intercept the mouse .
Maybe I'm a little confused .
Please correct me !
边栏推荐
- Basic use of continuous integration server Jenkins
- When using the benchmarksql tool to preheat data for kingbasees, execute: select sys_ Prewarm ('ndx_oorder_2 ') error
- 2022 tea master (intermediate) examination questions and tea master (intermediate) examination skills
- AWS VPC
- 4 years of experience to interview test development, 10 minutes to end, ask too
- Leecode swipe questions and record LCP 18 breakfast combination
- 会员积分商城系统的功能介绍
- [set theory] set concept and relationship (set family | set family examples | multiple sets)
- 540. Single element in ordered array
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
猜你喜欢

MC Layer Target

智能合约安全审计公司选型分析和审计报告资源下载---国内篇

GFS distributed file system (it's nice to meet it alone)

Design and implementation of JSP logistics center storage information management system

540. Single element in ordered array
![[NLP]—sparse neural network最新工作简述](/img/65/35ae0137f4030bdb2b0ab9acd85e16.png)
[NLP]—sparse neural network最新工作简述

Two points -leetcode-540 A single element in an ordered array

redis 持久化原理

Which Bluetooth headset is good about 400? Four Bluetooth headsets with strong noise reduction are recommended

Basic use of continuous integration server Jenkins
随机推荐
2022 registration of G2 utility boiler stoker examination and G2 utility boiler stoker reexamination examination
金仓数据库KingbaseES 插件kdb_exists_expand
After reviewing MySQL for a month, I was stunned when the interviewer of Alibaba asked me
Which Bluetooth headset is cost-effective? Four Bluetooth headsets with high cost performance are recommended
2022-02-12 (338. Bit count)
mysql字段userid逗号分开保存按userid查询
Fcpx template: sweet memory electronic photo album photo display animation beautiful memory
IPhone x forgot the boot password
FISCO bcos zero knowledge proof Fiat Shamir instance source code
Kingbasees plug-in KDB of Jincang database_ date_ function
Use the benchmarksql tool to perform a data prompt on kingbases. The jdbc driver cannot be found
220214c language learning diary
RSRS指标择时及大小盘轮动
The time has come for the domestic PC system to complete the closed loop and replace the American software and hardware system
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Solve BP Chinese garbled code
How to process the current cell with a custom formula in conditional format- How to address the current cell in conditional format custom formula?
When using the benchmarksql tool to test the concurrency of kingbasees, there are sub threads that are not closed in time after the main process is killed successfully
What functions need to be set after the mall system is built
Five elements of user experience