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 space is pressed). Then it sends the original string and a new transform string to the gui's labels.
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
import keyboard
import gui
def transform_string(string):
if len(string)>0:
return string[0:len(string)-1]
else:
return "0"
def logging_function():
string = ""
while(True):
event = keyboard.read_event()
if (str(event)[-5:-1] == "down"):
key_pressed = ((str(event).rsplit(' ', 1))[0])[14:]
if (key_pressed == "space"):
"""PASS THIS string AND transform_string TO OUR GUI's LABELS"""
print("Pass to gui's labels")
else:
string = string + key_pressed
logging_function()
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
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
app = QtWidgets.QApplication(sys.argv) # Create an instance of QtWidgets.QApplication
window = Ui() # Create an instance of our class
app.exec_() # Start the application
The ui window is the above: