Hi,
I am trying to create an app with 2 python files, the first reads the user input (either from python shell or directly from the keyboard via modules like pynput-keyboard) 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
<?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 tshown in the attached file.
I searched it and didn't find a solution. Any help is appreciated.
Thanks in advance.