【发布时间】:2021-12-02 22:47:14
【问题描述】:
我正在尝试创建一个包含 2 个 python 文件的应用程序,第一个读取用户输入(从 python shell 或通过诸如 pynput-keyboard 之类的模块直接从键盘)并将其存储在一个变量中(按下空格后) .然后它将原始字符串和一个新的转换字符串发送到 gui 的标签。
第二个创建具有 2 个标签和两个按钮的 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()
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
ui窗口如上:
【问题讨论】:
-
str(x+2)不会做任何事情,因为x是您程序中的字符串。先做x = int(input())。 -
很少有人想使用“输入”来读取数据。使用 QLineEdit、QSpinBox 等不是更好吗?也就是说,获取用户信息的 GUI 元素,好吧,如果你仍然想从控制台获取文本,那么你不应该使用“输入”,而是我在副本中建议的其他更 Qt 友好的方法。
-
你是对的,但我想解释一下,这个 gui 会根据用户输入更改标签,直接从键盘读取。我没有提供执行上述功能的代码,因为它会使问题变得更加复杂。输入未在 PyQt 窗口中读取。
-
@ppel123 1) 不要为我们简化问题,因为它会生成XY problem,2) 在重复的问题中,实现从控制台读取并以类似的方式将其显示在 QLabel 中对于你想用“input()”函数实现的那个,你指出了 pyinput,但是当没有看到任何相关的东西时,我不考虑它。请使用
@username -
@ppel123 从您提供的代码中,我了解到用户编写文本并存储字符,直到按下“空格”键,此时信息应发送到 GUI,但您有2 QLabels,据我了解,第一个“输入”被发送到第一个 QLabel,第二个“输入”被发送到第二个 QLabel 我正确吗?,如果按下 2“输入”后会发生什么用户继续输入并按第三个“输入”?这些信息是发送到哪个 QLabel 还是没有发送到任何地方?
标签: python python-3.x pyqt5 user-input