gpt4 book ai didi

python - 如何在 PySide 中自动刷新 TableView ?

转载 作者:行者123 更新时间:2023-12-01 04:32:13 27 4
gpt4 key购买 nike

我有下面的代码片段来从网站上删除数据并在表格 View 中显示结果。如何每 1 分钟或 30 秒更新或刷新一次表。无论如何可以在 pyside 中执行此操作吗?

import operator
from PySide.QtCore import *
from PySide.QtGui import *
from urllib import request
from lxml import etree

def getData():
url = "" # Removed valid url
response = request.urlopen(url)
html_parser = etree.HTMLParser()
tree = etree.parse(response, html_parser)
data_sets = tree.xpath("//div[@class='dataList']")
headers = ['Company Name','LTP','Change', '%Chg', 'Volume (lacs)', '30 Days % Change','365 Days % Change']
share_results = []

for element in data_sets:
result_list = []
share_comp_name = element.xpath('ul/li/p/a')[0]
result_list.append(share_comp_name.text)
share_value_list = element.xpath('ul/li/span')
counter = 0
for data in share_value_list:
result_list.append(float(str(data.text)))
counter += 1
share_results.append(tuple(result_list))
return headers, share_results

class MyWindow(QWidget):

def __init__(self, data_list, header, *args):
QWidget.__init__(self, *args)
self.setGeometry(300, 200, 550, 450)
self.setWindowTitle("Click on column title to sort")
table_model = MyTableModel(self, data_list, header)
table_view = QTableView()
table_view.setModel(table_model)
font = QFont("Verdana", 9)
table_view.setFont(font)
table_view.resizeColumnsToContents()
table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(table_view)
self.setLayout(layout)

class MyTableModel(QAbstractTableModel):

def __init__(self, parent, mylist, header, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.mylist = mylist
self.header = header

def rowCount(self, parent):
return len(self.mylist)

def columnCount(self, parent):
return len(self.header)

def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]

def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None

def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))


header, data_list = getData()
app = QApplication([])
win = MyWindow(data_list, header)
win.show()
app.exec_()

请帮忙。

最佳答案

使用Qtimer来更改Table view的模型。

import operator
from PySide.QtCore import *
from PySide.QtGui import *
from urllib import request
from lxml import etree
import sched, time

s = sched.scheduler(time.time, time.sleep)

def getData():
url = "" # Removed url
response = request.urlopen(url)
html_parser = etree.HTMLParser()
tree = etree.parse(response, html_parser)
data_sets = tree.xpath("//div[@class='dataList']")
headers = ['Company Name','LTP','Change', '%Chg', 'Volume (lacs)', '30 Days % Change','365 Days % Change']
share_results = []

for element in data_sets:
result_list = []
share_comp_name = element.xpath('ul/li/p/a')[0]
result_list.append(share_comp_name.text)
share_value_list = element.xpath('ul/li/span')
counter = 0
for data in share_value_list:
result_list.append(float(str(data.text)))
counter += 1
share_results.append(tuple(result_list))
return headers, share_results

class MyWindow(QWidget):

def __init__(self, *args):
QWidget.__init__(self, *args)
self.setGeometry(300, 200, 550, 450)
self.setWindowTitle("Top Shares")
self.table_model = MyTableModel(self)
self.table_view = QTableView()
self.table_view.setModel(self.table_model)
font = QFont("Verdana", 9)
self.table_view.setFont(font)
self.table_view.resizeColumnsToContents()
self.table_view.setSortingEnabled(True)
layout = QVBoxLayout(self)
layout.addWidget(self.table_view)
self.setLayout(layout)
# Added timer
timer = QTimer(self)
timer.timeout.connect(self.show_data)
timer.start(10000)

def show_data(self):
self.table_model = MyTableModel(self)
self.table_view.setModel(self.table_model)


class MyTableModel(QAbstractTableModel):
class_counter = 0
def __init__(self, parent, *args):
QAbstractTableModel.__init__(self, parent, *args)
self.header, self.mylist = getData()

def rowCount(self, parent):
return len(self.mylist)

def columnCount(self, parent):
return len(self.header)

def data(self, index, role):
if not index.isValid():
return None
elif role != Qt.DisplayRole:
return None
return self.mylist[index.row()][index.column()]

def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
return None

def sort(self, col, order):
"""sort table by given column number col"""
self.emit(SIGNAL("layoutAboutToBeChanged()"))
self.mylist = sorted(self.mylist,
key=operator.itemgetter(col))
if order == Qt.DescendingOrder:
self.mylist.reverse()
self.emit(SIGNAL("layoutChanged()"))

app = QApplication([])
win = MyWindow()
win.show()
win.repaint()
win.update()
app.exec_()

关于python - 如何在 PySide 中自动刷新 TableView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32219575/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com