Найти - Пользователи
Полная версия: PyQT while
Начало » GUI » PyQT while
1
missial
Имеется самописная функция, которая отображает значение среднего пинга.

def ping():
    address = 'ya.ru'
    while True:
        result=subprocess.Popen(["ping",'-n','1', str(address)],shell=True,stdout=subprocess.PIPE)
        ping = (result.stdout.read().decode('cp866'))
        m = ping.split()
        if len(m) != 48:
            b = '0'
        else:
            b = m[46]
В переменную b записывается среднее значение пинга.

Есть простейший GUI:
import sys
from PyQt4 import QtGui
class Absolute(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setWindowTitle('Communication')
        label = QtGui.QLabel('Hello', self)
        
app = QtGui.QApplication(sys.argv)
qb = Absolute()
qb.show()
sys.exit(app.exec_())
Как отобразить в label переменную b?
missial
import sys
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.label = QtGui.QLabel('Ping', self)
        self.button = QtGui.QPushButton('Start', self)
        self.button.clicked.connect(self.handleButton)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self._active = False
    def handleButton(self):
        if not self._active:
            self._active = True
            self.button.setText('Stop')
            QtCore.QTimer.singleShot(0, self.ping)
        else:
            self._active = False
    def closeEvent(self, event):
        self._active = False
    def ping(self):
        import subprocess
        address = 'ya.ru'
        while True:
            result=subprocess.Popen(["ping",'-n','1', str(address)],shell=True,stdout=subprocess.PIPE)
            ping = (result.stdout.read().decode('cp866'))
            m = ping.split()
            if len(m) != 48:
                b = '0'
            elif not self._active:
                break
            else:
                b = m[46]
            self.label.setText(str(b))
            QtGui.qApp.processEvents()
        self.button.setText('Start')
        self._active = False
if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
Нашел пример, подпилил. Работает.
missial
Как можно сделать отображение пинга в трее windows?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB