简体   繁体   中英

How to update PyQt5 label with unrounded variable value?

I keep track of some variables that are very small (smaller than 1) and would like to update a label on the screen with their value. However, the labels stay at 0 (sometimes they go to 1) even though the value of the variable is smaller than 1.

How do I tell my program to explicitly print an unrounded value?

Here is my code:

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_())

The variables in question are target_x_velocity , target_y_velocity , target_x_acceleration , and target_y_acceleration .

You must change %d to %f , the first shows only the whole part, while the second shows it in floating format. You can also change to "%.Xf" , where X is the number of decimal places to display.

More Information:

在此处输入图片说明

Complete Code:

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_())

Output:

在此处输入图片说明

In python 3.0 the % string formatting operator is replaced by a new system for string formatting operations. The % operator is still supported but deprecated since python 3.1, see What's new in Python 3 . Details see in PEP3101 and Format String Syntax .

I have implemented some examples of the new style syntax in your string, the first line in the simplest form of the new syntax, the braces are placeholders for the parameters of format() :

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

the second line with numbered parameters:

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

the third line parameters formatted as decimalnumber:

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

and the last line signed („+“ sign shown in every case, „-“ sign only shown is -)

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

for further details see links above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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