In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "how to add, delete, change and query data in PyQt5". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope that this article "how to add, delete, change and query data in PyQt5" can help you solve the problem.
Without saying much, let's first sort out the third-party modules we need.
In the UI interface layout part of PyQ5, it is enough to use these three modules.
From PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import *
Import the sys module into the code block for use in the body loop in the main function.
Import sys
Add_dialog is a self-written pop-up box to add data.
From add_dialog import AddDialog
Create a DataManage class, which is mainly used to implement the UI layout of the main window page.
Class DataManage (QWidget): def _ _ init__ (self): super (DataManage, self). _ _ init__ () self.data_list = [] self.init_ui () def init_ui (self):''Global Settings' self.setWindowIcon (QIcon ('data .ico')) self.setWindowTitle ('data Manager') self.resize Grid = QGridLayout ()''menu Settings' 'self.add_btn = QPushButton () self.add_btn.setText (' add data') self.add_btn.clicked.connect (self.add_btn_click) self.del_btn = QPushButton () self.del_btn.setText ('delete data') self.del_btn .clicked.connect (self.del_data_row) self.query_btn = QPushButton () self.query_btn.setText ('query') self.query_btn.clicked.connect (self.query_data_list)''data list Settings' self.data_table = QTableWidget () COLUMN = 5 ROW = 0 self.data_table.setColumnCount (COLUMN) Self.data_table.setRowCount (ROW) h_table_header = ['serial number' "name", "age", "class" Self.data_table.setHorizontalHeaderLabels (h_table_header) self.data_table.verticalHeader (). SetVisible (False) self.data_table.setShowGrid (True) self.data_table.setEditTriggers (QTableWidget.NoEditTriggers) self.data_table.setSelectionBehavior (QTableWidget.SelectRows) self.data_table.setSelectionMode (QTableWidget.SingleSelection) for index in range (self.data_table.columnCount (): headItem = self.data_table.horizontalHeaderItem (index) headItem.setTextAlignment (Qt.AlignVCenter)''add layout' 'grid.addWidget (self.add_btn 0,0,1,1) grid.addWidget (self.del_btn, 0,1,1) grid.addWidget (self.query_btn, 0,2,1,1) grid.addWidget (self.data_table, 1,0,1,3) self.setLayout (grid)
Define the required slot functions, through the signals of different buttons to bind the corresponding slot functions to achieve the events that the button needs to bind to achieve business logic.
# bind the button for the new data to the slot function def add_btn_click (self):''open the pop-up box module for the new data: return:' 'AddDialog.get_add_dialog (self) # bind the button for querying the data to the slot function def query_data_list (self):' query the data, And display the data in the data list in the main window: return:''data = self.data_list if len (data)! = 0 and len (data [0])! = 0: self.data_table.setRowCount (len (data)) self.data_table.setColumnCount (len (data [0])) for i in range (len ( Data): for j in range (len (data [0])): self.data_table.setItem (I J QTableWidgetItem (str (data [I] [j])) # bind the delete data button to the slot function def del_data_row (self):''delete the data information of a row: return:' 'row_select = self.data_table.selectedItems () print (row_select) if len (row_select)! = 0: row = row_select [0] .row () print (row) self.data_table.removeRow (row) del self.data_ list [row] print (self.data_table)
Start the entire application through the main () function.
If _ _ name__ = ='_ main__': app = QApplication (sys.argv) main = DataManage () main.show () sys.exit (app.exec_ ())
Finally, share the code of the custom pop-up module when you add data. This module is a module written separately to customize the bounce box, and the function of a bounce box can be realized by directly calling the pop-up box function of the module in the main window.
Create an add_dialog.py file and put the following code block in it.
From PyQt5.QtWidgets import * class AddDialog (QDialog): def _ _ init__ (self, parent=None): super (AddDialog, self). _ init__ (parent) self.init_ui (parent) def init_ui (self Parent):''horizontal layout' 'hbox = QHBoxLayout () self.save_btn = QPushButton () self.save_btn.setText (' save') self.save_btn.clicked.connect (lambda: self.save_btn_click (parent)) self.cancel_btn = QPushButton () self.cancel_btn.setText ('cancel') self.cancel _ btn.clicked.connect (self.cancel_btn_click) hbox.addWidget (self.save_btn) hbox.addWidget (self.cancel_btn)''form layout' 'fbox = QFormLayout () self.seq_lab = QLabel () self.seq_lab.setText (' serial number:') self.seq_text = QLineEdit () self.seq_text. SetPlaceholderText ('Please enter serial number') self.name_lab = QLabel () self.name_lab.setText ('name:') self.name_text = QLineEdit () self.name_text.setPlaceholderText ('Please enter name') self.age_lab = QLabel () self.age_lab.setText ('Age:') self.age_text = QLineEdit () Self.age_text.setPlaceholderText ('Please enter age') self.class_lab = QLabel () self.class_lab.setText ('Class:') self.class_text = QLineEdit () self.class_text.setPlaceholderText ('Please enter Class') self.socre_lab = QLabel () self.socre_lab.setText ('performance:') self.socre _ text = QLineEdit () self.socre_text.setPlaceholderText ('Please enter performance') fbox.addRow (self.seq_lab Self.seq_text) fbox.addRow (self.name_lab, self.name_text) fbox.addRow (self.age_lab, self.age_text) fbox.addRow (self.class_lab, self.class_text) fbox.addRow (self.socre_lab Self.socre_text) vbox = QVBoxLayout () vbox.addLayout (fbox) vbox.addLayout (hbox) self.setLayout (vbox) def save_btn_click (self Parent): if self.seq_text.text (). Strip ()! =''and self.name_text.text (). Strip ()! =''\ and self.age_text.text (). Strip ()! =''and self.class_text.text (). Strip ()! ='\ and self.socre_text.text (). Strip ()! = '': print (parent.data_list) data = [self.seq_text.text () Self.name_text.text (), self.age_text.text (), self.class_text.text () Self.socre_text.text ()] parent.data_list.append (data) print (parent.data_list) parent.query_data_list () self.close () def cancel_btn_click (self): self.close () @ staticmethod def get_add_dialog (parent=None): dialog = AddDialog ( Parent) return dialog.exec () on "how to add, delete, modify and query data in PyQt5" is introduced here. Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 234
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.