Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.36 KB | None | 0 0
  1. import time
  2. import random
  3. from PySide.QtCore import QObject, QThread, Signal, QFile, QTimer, Qt
  4. from PySide.QtGui import QGroupBox, QGridLayout, QTextEdit, QApplication
  5. from PySide.QtUiTools import QUiLoader
  6.  
  7.  
  8. class Worker(QThread):
  9.  
  10.     # This is the signal that we'll emit every time
  11.     # we load a new set of values from the url
  12.     new_value = Signal(str,str)
  13.  
  14.     def __init__(self, url, *args, **kwargs):
  15.         super(Worker, self).__init__(*args, **kwargs)
  16.         self.url = url
  17.         self.start()
  18.  
  19.     def run(self):
  20.         # Since we're going to run a mini event loop in the thread
  21.         # we can create and use a timer here
  22.         self.timer = QTimer()
  23.         self.timer.start(5000)
  24.         self.timer.timeout.connect(self.fetch_url)
  25.         # Start the thread's event loop
  26.         self.exec_()
  27.    
  28.     def fetch_url(self):
  29.         # Doesn't matter how long this takes because it only
  30.         # blocks the thread's event loop, not the GUI
  31.         time.sleep(random.random() * 2)
  32.         trades = str(random.random() * 25)
  33.         posts = str(random.random() * 100)
  34.  
  35.         # communicate the values via the signal
  36.         self.new_value.emit(trades, posts)
  37.  
  38.  
  39. def loadui(file_name):
  40.         loader = QUiLoader()
  41.         uifile = QFile(file_name)
  42.         uifile.open(QFile.ReadOnly)
  43.         ui = loader.load(uifile)
  44.         uifile.close()
  45.         return ui
  46.  
  47.  
  48.  
  49. if __name__ == "__main__":
  50.     import sys
  51.  
  52.     # Putting this in here because it requires access to MainWindow
  53.     # which only exists if this part of the code runs.
  54.     def create_topic(trades, posts):
  55.         box = QGroupBox()
  56.         grid = QGridLayout()
  57.         nickname = QTextEdit()
  58.    
  59.         box.setFixedHeight(200)
  60.         nickname.setText("%s : %s" % (trades, posts))
  61.  
  62.         grid.addWidget(nickname)
  63.         box.setLayout(grid)
  64.  
  65.         # Access global variable MainWindow, defined below
  66.         MainWindow.vlay.addWidget(box)
  67.  
  68.     app = QApplication(sys.argv)
  69.  
  70.     MainWindow = loadui("main.ui")
  71.     MainWindow.vlay.setAlignment(Qt.AlignTop)
  72.     timer = QTimer()
  73.     timer.start(5000)
  74.     id64 = '123'
  75.     worker = Worker('http://www.tf2outpost.com/user /' + id64)
  76.     worker.new_value.connect(create_topic)
  77.     app.aboutToQuit.connect(worker.quit)
  78.  
  79.     MainWindow.show()
  80.     app.exec_()
  81.     worker.quit()
  82.     worker.wait()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement