PyQt5 从 QLineEdit 到变量 [英] PyQt5 from QLineEdit to variable

查看:147
本文介绍了PyQt5 从 QLineEdit 到变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的 PyQt5 用户界面,如下所示:

I have created a simple PyQt5 User Interface that looks like this:

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) 用户在文本框"中写入一个字符串.

1) The user writes a string in the "textbox".

2) 点击运行按钮.

3) 运行按钮将字符串推送到另一个变量(假设x='whatevertheuserinputs'",它存储在另一个 python 文件中.

3) The run button pushes the string to another variable (let's say "x='whatevertheuserinputs'", which is stored in another python file.

谁能给我任何有关如何解决问题的提示?

Can anyone give me any hint on how to approach the problem?

非常感谢!

推荐答案

由于您还需要将字符串传递给另一个文件中的函数,因此准系统应用程序如下所示:

Since you also need the string to be passed to a function in another file, here is how a bare-bones application would be:

MyApp.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_())

StringStorage.py:

StringStorage.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

这篇关于PyQt5 从 QLineEdit 到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆