Python Forum
[PyQt] Python PyQt5 - Change label text dynamically based on user Input
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Python PyQt5 - Change label text dynamically based on user Input
#1
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.
Reply
#2
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to dynamically change radiobutton text kenwatts275 2 122 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  How to read text in kivy textinput or Label jadel440 1 242 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  Convert combobox user input in to date with tkinter Ame 8 1,616 Jul-01-2020, 09:40 PM
Last Post: Yoriz
  [PyQt] Increase text size and change color based on temp pav1983 5 584 Jun-22-2020, 10:52 PM
Last Post: menator01
  [Kivy] Kivy text label won't shows up! AVD_01 1 820 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [Tkinter] How do I change an image dynamically LeeMadeux 7 15,889 Jun-04-2020, 10:00 PM
Last Post: deanhystad
  [Tkinter] Change Label Every 5 Seconds gw1500se 4 1,086 May-26-2020, 05:32 PM
Last Post: gw1500se
  [Tkinter] Python 3 change label text gw1500se 6 1,324 May-08-2020, 05:47 PM
Last Post: deanhystad
  [PyQt] Control a combobox from another python file PyQt5 prath 0 629 May-05-2020, 03:22 PM
Last Post: prath
  [Tkinter] Change label for multiple frames Dandy_Don 3 647 Apr-30-2020, 02:22 PM
Last Post: Dandy_Don

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020