# encoding: utf-8 # 版权所有 2024 涂聚文有限公司 # 许可信息查看:言語成了邀功盡責的功臣,還需要行爲每日來值班嗎 # 描述: # Author : geovindu,Geovin Du 涂聚文. # IDE : PyCharm 2023.1 python 3.11 # os : windows 10 # database : mysql 9.0 sql server 2019, poostgreSQL 17.0 # Datetime : 2024/12/8 9:21 # User : geovindu # Product : PyCharm # Project : Pysimple # File : pyqt6tablepaging.py # explain : 学习 from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem, QPushButton class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Table with Paging") self.resize(300,300) # Sample data self.data = [ ['Alice', '25', 'New York'], ['Bob', '30', 'Los Angeles'], ['Charlie', '35', 'Chicago'], ['David', '40', 'San Francisco'], ['Emily', '28', 'Seattle'], ['Frank', '32', 'Boston'], ['Grace', '27', 'Miami'], ['Harry', '36', 'Dallas'], ['Ivy', '29', 'Atlanta'], ['Jack', '31', 'Denver'], ] self.page_size = 3 self.current_page = 0 self.rect=(2,4,200,280) self.table = QTableWidget() self.table.setColumnCount(3) self.table.setHorizontalHeaderLabels(['Name', 'Age', 'City']) self.table.geometry=self.rect self.rect2 = (2, 220, 220, 280) self.prev_button = QPushButton("Previous") self.next_button = QPushButton("Next") self.prev_button.clicked.connect(self.prev_page) self.next_button.clicked.connect(self.next_page) self.prev_button.geometry=self.rect2 layout = QVBoxLayout() layout.addWidget(self.table) layout.addWidget(self.prev_button) layout.addWidget(self.next_button) self.setLayout(layout) self.update_table() def update_table(self): start_index = self.current_page * self.page_size end_index = start_index + self.page_size self.table.setRowCount(0) for row_data in self.data[start_index:end_index]: row = self.table.rowCount() self.table.insertRow(row) for col, item in enumerate(row_data): self.table.setItem(row, col, QTableWidgetItem(item)) self.prev_button.setEnabled(self.current_page > 0) self.next_button.setEnabled(end_index < len(self.data)) def prev_page(self): self.current_page -= 1 self.update_table() def next_page(self): self.current_page += 1 self.update_table() if __name__ == '__main__': app = QApplication([]) window = MainWindow() window.show() app.exec()
标签:__,python,self,page,paging,table,prev,button From: https://www.cnblogs.com/geovindu/p/18593071