当前位置:网站首页>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 !
边栏推荐
- What functions need to be set after the mall system is built
- Feature_selection
- 拆一辆十万元的比亚迪“元”,快来看看里面的有哪些元器件。
- 540. Single element in ordered array
- FuncS sh file not found when using the benchmarksql tool to test kingbases
- Redis persistence principle
- 怎么用Kotlin去提高生产力:Kotlin Tips
- Some information about the developer environment in Chengdu
- Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
- How to use kotlin to improve productivity: kotlin tips
猜你喜欢
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
C language series - Section 3 - functions
Employee attendance management system based on SSM
Arthas watch grabs a field / attribute of the input parameter
2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank
How to choose cross-border e-commerce multi merchant system
Which code editor is easy to use? Code editing software recommendation
智能合约安全审计公司选型分析和审计报告资源下载---国内篇
解决bp中文乱码
Data Lake three swordsmen -- comparative analysis of delta, Hudi and iceberg
随机推荐
Kingbasees plug-in KDB of Jincang database_ database_ link
[no title] 2022 chlorination process examination content and free chlorination process examination questions
Basic use of continuous integration server Jenkins
[fairseq] 报错:TypeError: _broadcast_coalesced(): incompatible function arguments
Jincang KFS data bidirectional synchronization scenario deployment
Golang -- realize file transfer
Wine travel Jianghu War: Ctrip is strong, meituan is strong, and Tiktok is fighting
Five elements of user experience
[set theory] set identities (idempotent law | exchange law | combination law | distribution rate | De Morgan law | absorption rate | zero law | identity | exclusion law | contradiction law | complemen
Kubernetes源码分析(一)
How to use kotlin to improve productivity: kotlin tips
540. Single element in ordered array
[set theory] ordered pair (ordered pair | ordered triple | ordered n ancestor)
金仓数据库KingbaseES 插件kdb_database_link
[set theory] binary relationship (special relationship type | empty relationship | identity relationship | global relationship | divisive relationship | size relationship)
JS realizes the animation effect of text and pictures in the visual area
Xrandr modifier la résolution et le taux de rafraîchissement
[Chongqing Guangdong education] reference materials for design and a better life of Zhongyuan Institute of science and technology
重绘和回流
xrandr修改分辨率与刷新率