简体   繁体   English

刷新PyQt4窗口

[英]Refreshing PyQt4 window

Hey I am trying to refresh the GUI window, but for some reason it does not refresh till all of the loops are finished. 嘿,我正在尝试刷新GUI窗口,但是由于某种原因,它直到所有循环完成后才刷新。 I read the other posts and tried their solutions but they didnt help. 我阅读了其他帖子并尝试了他们的解决方案,但他们没有帮助。 Can someone spot the thing that I did wrong or what I should add? 有人可以发现我做错的事情或应该添加的内容吗? The code is below, reads certain values from a json file and stores them in the GUI table. 下面的代码,从json文件中读取某些值,并将它们存储在GUI表中。 I am using PyQt4 and python 2.7. 我正在使用PyQt4和python 2.7。 Thank you in advance. 先感谢您。

import sys
from PyQt4 import QtCore, QtGui
import time
from PyQt4.QtGui import QApplication
import json
import threading

from GUI_Window import *
LastCommandTime=0

class MyForm(QtGui.QMainWindow):
   def __init__(self, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.ui = Ui_MainWindow()
      self.ui.setupUi(self)
      self.ui.button.clicked.connect(self.DataCollection)
   def DataCollection(self):
      print "button clicked"
      task1=threading.Thread(target=self.DisplayCommand)
      task1.start()
      task2=threading.Thread(target=self.DisplayTemp)
      task2.start()
      task1.join()
      task2.join()
   def DisplayCommand(self):
      start=time.time()
      with open("20170602153617003.log") as f:
         for line in f:
            data = []
            data=json.loads(line)
            try:
                if data['TYPE']== "             cmd":
                    command= data["command"]
                    print "1"
                    self.ui.command_table.setItem(0,0, QtGui.QTableWidgetItem(command["cmd_name"]))
                    self.ui.command_table.setItem(0,1, QtGui.QTableWidgetItem(command["opcode"]))
                    self.ui.command_table.setItem(0,2, QtGui.QTableWidgetItem("".join((str(x)+',') for x in command["param_type_list"])))
                    self.ui.command_table.setItem(0,3, QtGui.QTableWidgetItem("".join((str(x)+',') for x in command["param_val_list"])))
                    self.ui.commands_time.display((time.time()-start))
                    start=time.time()
                    time.sleep(1)
                if data['TYPE']== "       cmd_reply":
                    command= data["response"]
                    self.ui.reply_table.setItem(0,0, QtGui.QTableWidgetItem(data["cmd_name"].replace(" ","")))
                    self.ui.reply_table.setItem(0,1, QtGui.QTableWidgetItem("0x%02x"%command["opcode"]))
                    self.ui.reply_table.setItem(0,2, QtGui.QTableWidgetItem(str(command["error_control_type"])))
                    self.ui.reply_table.setItem(0,3, QtGui.QTableWidgetItem(str(command["data_present"])))
                    self.ui.reply_table.setItem(0,4, QtGui.QTableWidgetItem(str(command["command_reply"])))
                    self.ui.reply_table.setItem(0,5, QtGui.QTableWidgetItem(str(command["status_flags"])))
                    self.ui.reply_table.setItem(0,6, QtGui.QTableWidgetItem(str(command["condition_code"])))
                    self.ui.reply_table.setItem(0,7, QtGui.QTableWidgetItem(str(command["data_length"])))
                    self.ui.reply_table.setItem(0,8, QtGui.QTableWidgetItem(str(command["data"])))
                    self.ui.reply_table.setItem(0,9, QtGui.QTableWidgetItem(str(command["error_control"])))
                    flags=command['flags']
                    self.ui.replyflg_table.setItem(0,0, QtGui.QTableWidgetItem(str(flags["BOOT_FAIL"])))
                    self.ui.replyflg_table.setItem(0,1, QtGui.QTableWidgetItem(str(flags["BOOT_SOURCE"])))
                    self.ui.replyflg_table.setItem(0,2, QtGui.QTableWidgetItem(str(flags["COMM_SIDE"])))
                    self.ui.replyflg_table.setItem(0,3, QtGui.QTableWidgetItem(str(flags["SELF_TEST_FAIL"])))
                    self.ui.replyflg_table.setItem(0,4, QtGui.QTableWidgetItem(str(flags["STA_ERROR"])))
                    self.ui.replyflg_table.setItem(0,5, QtGui.QTableWidgetItem(str(flags["SPECTROMETER_ERROR"])))
                    self.ui.replyflg_table.setItem(0,6, QtGui.QTableWidgetItem(str(flags["SCCD_READY"])))
                    self.ui.replyflg_table.setItem(0,7, QtGui.QTableWidgetItem(str(flags["SCANNER_ON"])))
                    self.ui.replyflg_table.setItem(0,8, QtGui.QTableWidgetItem(str(flags["SCANNER_COMM_ERROR"])))
                    self.ui.replyflg_table.setItem(0,9, QtGui.QTableWidgetItem(str(flags["SCANNER_HOME_ERROR"])))
                    self.ui.replyflg_table.setItem(0,10, QtGui.QTableWidgetItem(str(flags["ERROR_LOG_NOT_EMPTY"])))
                    self.ui.replyflg_table.setItem(0,11, QtGui.QTableWidgetItem(str(flags["SEND_EVR"])))
                    self.ui.replyflg_table.setItem(0,12, QtGui.QTableWidgetItem(str(flags["SYS_ERROR"])))
                    self.ui.replyflg_table.setItem(0,13, QtGui.QTableWidgetItem(str(flags["UNDEFINED0"])))
                    self.ui.replyflg_table.setItem(0,14, QtGui.QTableWidgetItem(str(flags["UNDEFINED1"])))
                time.sleep(.1)
            except KeyError:
                time.sleep(.1)
                pass
            QApplication.processEvents() 

    def DisplayTemp(self):
       i=0
       while i<10:
           print "hi"
           self.ui.flt_table.setItem(0,0, QtGui.QTableWidgetItem(str(i)))
           i=i+1
           QApplication.processEvents() 
           time.sleep(.2)




if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = MyForm()
    graphTimer = QtCore.QTimer()
    graphTimer.timeout.connect(app.processEvents)
    graphTimer.start(0.1)
    myapp.show()
    sys.exit(app.exec_())

Also it appears that if I comment out the DisplayTemp() then it works just fine. 同样,如果我将DisplayTemp()注释掉,它也可以正常工作。 I was thinking that the number of threads might be causing trouble but I am not sure how to fix it. 我当时以为线程数可能会引起麻烦,但是我不确定如何解决。

Apparently it was caused by the .join commands. 显然是由.join命令引起的。 They are blocking so apparently they stop the GUI from refreshing at some level. 它们正在阻塞,因此显然在某种程度上阻止了GUI的刷新。 I hope this helps someone out in the future. 我希望这对以后的人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM