Reputation: 93
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:
Upvotes: 2
Views: 2461
Reputation: 243955
What you have to do is use the pynput listener and analyze the keycode, then according to that build the word and send it by means of a signal to the GUI that according to its own logic add it to the QLabels:
read_user_input.py
from pynput.keyboard import Key, Listener
from PyQt5 import QtCore
class KeyMonitor(QtCore.QObject):
wordPressed = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.listener = Listener(on_release=self.on_release)
self.word = ""
def on_release(self, key):
if hasattr(key, "char"):
self.word += key.char
if key == Key.space:
self.wordPressed.emit(self.word)
self.word = ""
def stop_monitoring(self):
self.listener.stop()
def start_monitoring(self):
self.listener.start()
gui.py
from itertools import cycle
import sys
from PyQt5 import QtCore, QtWidgets, uic
from read_user_input import KeyMonitor
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi("untitled.ui", self)
self.show()
self.labels = cycle([self.label, self.label_2])
@QtCore.pyqtSlot(str)
def on_word_pressed(self, word):
le = next(self.labels)
le.setText(word)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Ui()
monitor = KeyMonitor()
monitor.wordPressed.connect(window.on_word_pressed)
monitor.start_monitoring()
sys.exit(app.exec_())
If you want to use the keyboard library then you must use the following:
read_user_input.py
import threading
import keyboard
from PyQt5 import QtCore
class KeyMonitor(QtCore.QObject):
wordPressed = QtCore.pyqtSignal(str)
def __init__(self, parent=None):
super().__init__(parent)
self.word = ""
self.listener = threading.Thread(target=self._task, daemon=True)
def _task(self):
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
if event.name == "space":
self.wordPressed.emit(self.word)
self.word = ""
elif len(event.name) == 1:
self.word += event.name
def start_monitoring(self):
self.listener.start()
The logic of modifying the data is a trivial task that consists of converging the "word" and generating the new strings:
import sys
from PyQt5 import QtCore, QtWidgets, uic
from read_user_input import KeyMonitor
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
uic.loadUi("untitled.ui", self)
self.show()
@QtCore.pyqtSlot(str)
def on_word_pressed(self, word):
try:
x = int(word)
except ValueError:
print("You must enter a whole number")
else:
self.label.setText("{}".format(x + 2))
self.label_2.setText("Changed {}".format(x - 2))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Ui()
monitor = KeyMonitor()
monitor.wordPressed.connect(window.on_word_pressed)
monitor.start_monitoring()
sys.exit(app.exec_())
Upvotes: 3