当前位置:网站首页>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 !
边栏推荐
- [no title] 2022 chlorination process examination content and free chlorination process examination questions
- Feature_selection
- Kingbasees plug-in KDB of Jincang database_ exists_ expand
- Reptile exercise 02
- 使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
- IPhone x forgot the boot password
- 2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test
- Interface in TS
- What are the Bluetooth headsets with good sound quality in 2022? Inventory of four high-quality Bluetooth headsets
- Two points -leetcode-540 A single element in an ordered array
猜你喜欢

How to retrieve the password for opening word files

2022 t elevator repair simulation examination question bank and t elevator repair simulation examination question bank

Competitive product analysis and writing

Library management system based on SSM

BMZCTF simple_ pop

2022 a special equipment related management (elevator) analysis and a special equipment related management (elevator) simulation test

C language series - Section 3 - functions

How to choose cross-border e-commerce multi merchant system

Pdf editing tool movavi pdfchef 2022 direct download

Fcpx template: sweet memory electronic photo album photo display animation beautiful memory
随机推荐
FFMpeg filter
What functions need to be set after the mall system is built
Ffmpeg mix
What's wrong with SD card data damage? How to recover SD card data damage
Fcpx template: sweet memory electronic photo album photo display animation beautiful memory
[set theory] binary relation (example of binary relation on a | binary relation on a)
Database management tool, querious direct download
Redis persistence principle
使用BENCHMARKSQL工具对KingbaseES预热数据时执行:select sys_prewarm(‘NDX_OORDER_2 ‘)报错
多板块轮动策略编写技巧----策略编写学习教材
Know that Chuangyu cloud monitoring - scanv Max update: Ecology OA unauthorized server request forgery and other two vulnerabilities can be detected
Daily question - ugly number
[set theory] inclusion exclusion principle (including examples of exclusion principle)
Xrandr modifier la résolution et le taux de rafraîchissement
I've been in software testing for 8 years and worked as a test leader for 3 years. I can also be a programmer if I'm not a professional
[fxcg] inflation differences will still lead to the differentiation of monetary policies in various countries
Xrandr modify resolution and refresh rate
[NLP]—sparse neural network最新工作简述
Busycal latest Chinese version
Dive into deep learning - 2.1 data operation & Exercise