python - PyQt5 从 QLineEdit 到变量
问题描述
我创建了一个简单的 PyQt5 用户界面,如下所示:
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QLineEdit
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Test'
self.left = 10
self.top = 10
self.width = 400
self.height = 500
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.move(20, 100)
self.textbox.resize(280,40)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.move(20, 200)
self.textbox.resize(280,40)
# Create a button in the window
self.button = QPushButton('Run', self)
self.button.move(300, 99)
# Create a button in the window
self.button = QPushButton('Run', self)
self.button.move(300, 199)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
我想做的是:
1)用户在“文本框”中写入一个字符串。
2) 单击运行按钮。
3)运行按钮将字符串推送到另一个变量(假设“x ='whatevertheuserinputs'”,它存储在另一个python文件中。
谁能给我任何关于如何解决这个问题的提示?
提前非常感谢!
解决方案
由于您还需要将字符串传递给另一个文件中的函数,因此这是一个基本应用程序的样子:
我的应用程序.py:
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget, QLineEdit
# For passing the string to some function in another file:
from StringStorage import storeString
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Test'
self.left = 10
self.top = 10
self.width = 400
self.height = 500
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Create textbox
self.textbox = QLineEdit(self)
self.textbox.move(20, 100)
self.textbox.resize(280,40)
# Create a button in the window
self.button = QPushButton('Run', self)
self.button.move(300, 99)
# When the 'clicked' signal of the button is emitted, call some function (which acts as a slot):
self.button.clicked.connect(self.onButtonClicked)
self.show()
# Function to pass the entered string along to the function from another file
def onButtonClicked(self):
storeString(self.textbox.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
字符串存储.py:
# The text is to be stored in this string
x = ''
def storeString(inString):
global x
x = inString
# Do something with the string
print(inString)
return
推荐阅读
- babeljs - 使用自定义 babel 插件构建聚合物?
- python - 带有 WSL 的 Pycharm Professional:无法读取使用 ~/.profile 设置的环境变量
- javascript - 如何读取从前端发送的 JSON 字符串
- python - 无法在 Python 的命令行中修复 X
- javascript - ngClass 神秘地不适用于 Angular 6?
- javascript - 我如何确定是否已经使用 Angular 6 缓存了特定图像
- python - requests.exceptions.HTTPError:400 客户端错误:对 url 的错误请求:https://accounts.spotify.com/api/token
- quicksort - 学习快速排序算法
- ruby-on-rails - Rails:按联接表分组
- sql-server - SQL Server 仅还原架构