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


Messages In This Thread
Python PyQt5 - Change label text dynamically based on user Input - by ppel123 - Mar-19-2020, 08:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't change the colour of Tk button text Pilover 6 14,733 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] [Solved]Change text color of one line in TextBrowser Extra 2 4,886 Aug-23-2022, 09:11 PM
Last Post: Extra
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,852 Jun-26-2022, 06:26 PM
Last Post: menator01
  [WxPython] [SOLVED] How to change button label? Winfried 3 2,084 May-31-2022, 06:37 PM
Last Post: Winfried
  [PyGUI] [Solved]Help storing in user input from line edit Extra 2 1,730 May-12-2022, 07:46 PM
Last Post: Extra
  Can't get tkinter button to change color based on changes in data dford 4 3,417 Feb-13-2022, 01:57 PM
Last Post: dford
  tkinter change the text of the checkbox zazas321 1 3,828 Sep-17-2021, 06:19 AM
Last Post: zazas321
Question Python 3.10 and PyQt5(6) zazen000 2 7,696 Jun-14-2021, 05:02 PM
Last Post: deanhystad
  Updating button text based upon different variable values knoxvilles_joker 0 2,232 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  update text variable on label with keypress knoxvilles_joker 3 4,903 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker

Forum Jump:

User Panel Messages

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