Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import random
- from PySide.QtCore import QObject, QThread, Signal, QFile, QTimer, Qt
- from PySide.QtGui import QGroupBox, QGridLayout, QTextEdit, QApplication
- from PySide.QtUiTools import QUiLoader
- class Worker(QThread):
- # This is the signal that we'll emit every time
- # we load a new set of values from the url
- new_value = Signal(str,str)
- def __init__(self, url, *args, **kwargs):
- super(Worker, self).__init__(*args, **kwargs)
- self.url = url
- self.start()
- def run(self):
- # Since we're going to run a mini event loop in the thread
- # we can create and use a timer here
- self.timer = QTimer()
- self.timer.start(5000)
- self.timer.timeout.connect(self.fetch_url)
- # Start the thread's event loop
- self.exec_()
- def fetch_url(self):
- # Doesn't matter how long this takes because it only
- # blocks the thread's event loop, not the GUI
- time.sleep(random.random() * 2)
- trades = str(random.random() * 25)
- posts = str(random.random() * 100)
- # communicate the values via the signal
- self.new_value.emit(trades, posts)
- def loadui(file_name):
- loader = QUiLoader()
- uifile = QFile(file_name)
- uifile.open(QFile.ReadOnly)
- ui = loader.load(uifile)
- uifile.close()
- return ui
- if __name__ == "__main__":
- import sys
- # Putting this in here because it requires access to MainWindow
- # which only exists if this part of the code runs.
- def create_topic(trades, posts):
- box = QGroupBox()
- grid = QGridLayout()
- nickname = QTextEdit()
- box.setFixedHeight(200)
- nickname.setText("%s : %s" % (trades, posts))
- grid.addWidget(nickname)
- box.setLayout(grid)
- # Access global variable MainWindow, defined below
- MainWindow.vlay.addWidget(box)
- app = QApplication(sys.argv)
- MainWindow = loadui("main.ui")
- MainWindow.vlay.setAlignment(Qt.AlignTop)
- timer = QTimer()
- timer.start(5000)
- id64 = '123'
- worker = Worker('http://www.tf2outpost.com/user /' + id64)
- worker.new_value.connect(create_topic)
- app.aboutToQuit.connect(worker.quit)
- MainWindow.show()
- app.exec_()
- worker.quit()
- worker.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement