Python Forum

Full Version: Python PyQt5 - Change label text dynamically based on user Input
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I am trying to create an app with 2 python files, the first reads the user input (either from python shell or from other source) and stores it in a variable (after enter pressed).

The second creates the gui that has 2 labels and two buttons and takes the variable passed from the first file and changes the labels based on this variable (the buttons are used for data insert in a later step in a database).

I have created the gui and the python script that reads the input, but I am struggling on passing this variable to the second script and on changing the label dynamically.
Please see the code samples above.

read_user_input.py
from gui import Ui
from PyQt5 import QtWidgets, uic
import sys

app = QtWidgets.QApplication(sys.argv) # Create an instance of QtWidgets.QApplication
window = Ui() # Create an instance of our class
# app.exec_() # Start the application

x = input("Give a number")
if (int(x) == 2):
    window.label.setText(str(x+2))
else:
    window.label_2.setText("changed "+str(x-2))
gui.ui
Quote:<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Hook</class>
<widget class="QMainWindow" name="Hook">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>237</width>
<height>120</height>
</rect>
</property>
<property name="windowTitle">
<string>Hook</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>10-03-2020 thesis_01/keyboard.ico</normaloff>10-03-2020 thesis_01/keyboard.ico</iconset>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>10</x>
<y>0</y>
<width>131</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>Label1</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>10</x>
<y>40</y>
<width>131</width>
<height>31</height>
</rect>
</property>
<property name="mouseTracking">
<bool>false</bool>
</property>
<property name="text">
<string>Label2</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>160</x>
<y>0</y>
<width>61</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>B1</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton_2">
<property name="geometry">
<rect>
<x>160</x>
<y>42</y>
<width>61</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>B2</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>237</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuHook">
<property name="title">
<string>Hook</string>
</property>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
</widget>
<addaction name="menuHook"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>

gui.py
from PyQt5 import QtWidgets, uic
import sys
import time

class Ui(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui, self).__init__() # Call the inherited classes __init__ method
        uic.loadUi('untitled.ui', self) # Load the .ui file
        self.show() # Show the GUI
The ui window is shown in the attached link.
( https://ibb.co/0rKxQQV )

I searched it and didn't find a solution. Any help is appreciated.
Thanks in advance.
I don't have PyQt5 installed and I'm not installing a package just to answer a question in the forum, but I do have PySide2 and I was able to user your gui.ui file. This is an interesting way to make windows.

First off, there really isn't any passing variables from one file into another. You have a script file named read_user_input.py and a module named gui.py. I find the module most interesting so I'll start with that.

Here is the code that I wrote to build your panel
import sys
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication
from PySide2.QtCore import QFile

if __name__ == "__main__":
    app = QApplication(sys.argv)

    ui_file = QFile("gui.ui")
    ui_file.open(QFile.ReadOnly)

    loader = QUiLoader()
    window = loader.load(ui_file)
    ui_file.close()
    window.show()

    x = int(input("Give a number"))
    if (int(x) == 2):
        window.label.setText(str(x+2))
    else:
        window.label_2.setText("changed "+str(x-2))

    sys.exit(app.exec_())
You may notice that I put the code that reads the uic file and makes the window in the same file as the script. What your code does with that class file is kind of clever. I'm guessing you found the code here:

https://nitratine.net/blog/post/how-to-i...ython-gui/

Or maybe this is another one of those python examples that gets copied over and over until searching returns 20 pages all saying the same thing. If I used uic files a lot I might use this same idea, but write the class like this:
class Ui(QtWidgets.QMainWindow):
    def __init__(self, uic_file):
        super(Ui, self).__init__()
        uic.loadUi(uic_file, self)
        self.show()
This class does the same thing as ui_file and loader code in the example above. It looks different partially because Qt5 and PySide2 do the same thing different ways, but also because this nifty Ui class hides the ugliness of the loader. To make a window all I need is a uic file and a program that calls:
    window = Ui(uic_file_name)
See your first error yet? The class creates a new window by loading a uic file. Your uic file is named "gui.ui". If you look at your code you may notice that the gui.py file loads a uic file named "untitled.ui". That obviously isn't going to work. You probably would have noticed this right away if the class wasn't being so clever about hiding those kinds of details.

After I got past that problem I ran into another. What is returned when you make the call:
    x = input("Give a number")
If I type "4" what is x? It is not the number 4, it is the string "4". You obviously know this because you convert x to an integer before comparing "==2", but you forget moments later when you try to add a string and an integer. What is "4"-2? When I run it is an error.

The last issue I ran into is that you commented out "app.exec". When I ran your script the window popped up for a moment and disappeared. If I run the script from inside Idle the window sticks around. You probably want to be able to run your script outside of idle, so you should put app.exec back in. Did you comment it out because it blocks until the window is closed?