我正在尝试用2个python文件创建一个应用程序,第一个读取用户输入(从python shell或直接通过键盘通过pynput-keyboard之类的模块)并将其存储在变量中(按空格键后)。然后,它将原始字符串和新的转换字符串发送到gui的标签。
第二个创建具有两个标签和两个按钮的gui,并获取从第一个文件传递来的变量,并基于此变量更改标签(这些按钮用于在数据库的后续步骤中插入数据)。
我已经创建了gui和python脚本来读取输入,但是我正在努力将此变量传递给第二个脚本并动态更改标签。
请参阅上面的代码示例。
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()
归义
<?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
ui窗口是上面的:
问题来源:stackoverflow
您要做的是使用Pynput侦听器并分析键码,然后根据该键构建单词并将其通过信号发送到GUI,然后根据其自身的逻辑将其添加到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_()) 更新:
如果要使用键盘库,则必须使用以下内容:
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()
更新:
修改数据的逻辑是一项琐碎的任务,包括聚合“单词”并生成新字符串:
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_())
回答来源:stackoverflow
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。