如何使用未变量值更新PyQt5标签?

时间:2017-01-20 18:22:11

标签: python qt python-3.x pyqt pyqt5

我会跟踪一些非常小(小于1)的变量,并希望使用它们的值更新屏幕上的标签。但是,即使变量的值小于1,标签仍保持为0(有时它们会变为1)。

如何告诉我的程序明确打印未设置的值?

这是我的代码:

import sys
import random
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter


class MouseTracker(QWidget):
    distance_from_target = 0
    mouse_x_pos = 0
    mouse_y_pos = 0
    target_x_pos = 0
    target_y_pos = 0
    target_x_velocity = 0
    target_y_velocity = 0
    target_x_acceleration = 0
    target_y_acceleration = 0

    def __init__(self, parent=None):
        super(MouseTracker, self).__init__(parent=parent)
        self.initUI()
        self.setMouseTracking(True)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.changePosition)
        self.timer.start(1)

    def changePosition(self):
        random.seed()
        self.target_x_acceleration = random.randint(-10, 10) / 100
        self.target_y_acceleration = random.randint(-10, 10) / 100

        self.target_x_velocity += self.target_x_acceleration
        self.target_y_velocity += self.target_y_acceleration

        self.target_x_pos += self.target_x_velocity
        self.target_y_pos += self.target_y_velocity

        self.distance_from_target = round(
            ((self.mouse_y_pos - self.target_y_pos) ** 2 + (self.mouse_x_pos - self.target_x_pos) ** 2) ** 0.5)
        self.label.setText(
            'Mouse: (%d : %d)' % (self.mouse_x_pos, self.mouse_y_pos) + "   Distance from target: " + str(self.distance_from_target)
            + "\nTarget position: (%d : %d)" % (self.target_x_pos, self.target_y_pos)
            + "\nTarget velocity: (%d : %d)" % (self.target_x_velocity, self.target_y_velocity)
            + "\nTarget acceleration: (%d : %d)" % (self.target_x_acceleration, self.target_y_acceleration))
        self.update()

    def initUI(self):
        self.setGeometry(200, 200, 1000, 500)
        self.setWindowTitle('Mouse Tracker')
        self.label = QLabel(self)
        self.label.resize(500, 100)
        self.show()

    def mouseMoveEvent(self, event):
        self.mouse_x_pos = event.x()
        self.mouse_y_pos = event.y()
        self.update()

    def mousePressEvent(self, event):
        self.target_x_velocity = 0
        self.target_y_velocity = 0
        self.target_x_acceleration = 0
        self.target_y_acceleration = 0 
        self.target_x_pos = event.x()
        self.target_y_pos = event.y()
        self.distance_from_target = round(
            ((self.mouse_y_pos - self.target_y_pos) ** 2 + (self.mouse_x_pos - self.target_x_pos) ** 2) ** 0.5)
        self.update()

    def paintEvent(self, event):
        q = QPainter()
        q.begin(self)
        q.drawLine(self.mouse_x_pos, self.mouse_y_pos, self.target_x_pos, self.target_y_pos)
        q.drawEllipse(self.target_x_pos - self.distance_from_target, self.target_y_pos - self.distance_from_target, self.distance_from_target*2, self.distance_from_target*2)

app = QApplication(sys.argv)
w = MouseTracker()
sys.exit(app.exec_())

有问题的变量有target_x_velocitytarget_y_velocitytarget_x_accelerationtarget_y_acceleration

2 个答案:

答案 0 :(得分:1)

您必须将%d更改为%f,第一个仅显示整个部分,而第二个显示为浮动格式。您也可以更改为"%.Xf",其中X是要显示的小数位数。

更多信息:

enter image description here

完整代码:

import sys
import random
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import (QApplication, QLabel, QWidget)
from PyQt5.QtGui import QPainter


class MouseTracker(QWidget):
    distance_from_target = 0
    mouse_x_pos = 0
    mouse_y_pos = 0
    target_x_pos = 0
    target_y_pos = 0
    target_x_velocity = 0
    target_y_velocity = 0
    target_x_acceleration = 0
    target_y_acceleration = 0

    def __init__(self, parent=None):
        super(MouseTracker, self).__init__(parent=parent)
        self.initUI()
        self.setMouseTracking(True)
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.changePosition)
        self.timer.start(1)

    def changePosition(self):
        random.seed()
        self.target_x_acceleration = random.randint(-10, 10) / 100
        print(self.target_x_acceleration)
        self.target_y_acceleration = random.randint(-10, 10) / 100

        self.target_x_velocity += self.target_x_acceleration
        self.target_y_velocity += self.target_y_acceleration

        self.target_x_pos += self.target_x_velocity
        self.target_y_pos += self.target_y_velocity

        self.distance_from_target = round(
            ((self.mouse_y_pos - self.target_y_pos) ** 2 + (self.mouse_x_pos - self.target_x_pos) ** 2) ** 0.5)
        self.label.setText(
            'Mouse: (%d : %d)' % (self.mouse_x_pos, self.mouse_y_pos) + "   Distance from target: " + str(self.distance_from_target)
            + "\nTarget position: (%d : %d)" % (self.target_x_pos, self.target_y_pos)
            + "\nTarget velocity: (%f : %f)" % (self.target_x_velocity, self.target_y_velocity)
            + "\nTarget acceleration: (%f : %f)" % (self.target_x_acceleration, self.target_y_acceleration))
        self.update()

    def initUI(self):
        self.setGeometry(200, 200, 1000, 500)
        self.setWindowTitle('Mouse Tracker')
        self.label = QLabel(self)
        self.label.resize(500, 100)
        self.show()

    def mouseMoveEvent(self, event):
        self.mouse_x_pos = event.x()
        self.mouse_y_pos = event.y()
        self.update()

    def mousePressEvent(self, event):
        self.target_x_velocity = 0
        self.target_y_velocity = 0
        self.target_x_acceleration = 0
        self.target_y_acceleration = 0 
        self.target_x_pos = event.x()
        self.target_y_pos = event.y()
        self.distance_from_target = round(
            ((self.mouse_y_pos - self.target_y_pos) ** 2 + (self.mouse_x_pos - self.target_x_pos) ** 2) ** 0.5)
        self.update()

    def paintEvent(self, event):
        q = QPainter()
        q.begin(self)
        q.drawLine(self.mouse_x_pos, self.mouse_y_pos, self.target_x_pos, self.target_y_pos)
        q.drawEllipse(self.target_x_pos - self.distance_from_target, self.target_y_pos - self.distance_from_target, self.distance_from_target*2, self.distance_from_target*2)

app = QApplication(sys.argv)
w = MouseTracker()
sys.exit(app.exec_())

输出:

enter image description here

答案 1 :(得分:1)

在python 3.0中,%字符串格式化运算符被替换为字符串格式化操作的新系统。仍支持%运算符,但自python 3.1以来已弃用,请参阅What's new in Python 3。详情见PEP3101Format String Syntax

我在你的字符串中实现了一些新样式语法的例子,新语法最简单形式的第一行,大括号是format()参数的占位符:

'Mouse: ({} : {})'.format(self.mouse_x_pos, self.mouse_y_pos) + "   Distance from target: " + str(self.distance_from_target)

带编号参数的第二行:

+ "\nTarget position: ({0} : {1})".format(self.target_x_pos, self.target_y_pos)

格式化为十进制数的第三行参数:

+ "\nTarget velocity: ({:.4f} : {:.8f})".format(self.target_x_velocity, self.target_y_velocity)

并且最后一行签名(每种情况下都显示“+”符号,“ - ”仅显示符号 - )

+ "\nTarget acceleration: ({0:+.6f} : {1:-.8f})".format(self.target_x_acceleration, self.target_y_acceleration))

有关详细信息,请参阅上面的链接