0

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

What I am trying to do is:

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

2) Clicks the run button.

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?

Thanks so much in advance!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
iraciv94
  • 782
  • 2
  • 11
  • 26

1 Answers1

1

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:

# 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
mahesh
  • 1,028
  • 10
  • 24
  • Hi Mahesh, thanks for your help; However, if I do as I tell you, the program prints correctly in the python console whatever I write on the GUI, but it doesn't store it as a value in the "variable section" of the StringStorage.py file. – iraciv94 Jul 13 '18 at 09:09
  • You can declare a variable there, like this: `x = inString`. Then, variable x will contain the value of `inString`, which is what the user input. I will make an edit to include this. – mahesh Jul 13 '18 at 09:53
  • So, I tried to use the modified code, you gave me. The issue is that: - if I run the StringStorage.py it saves the variable as the empty string x defined at the beginning - if I run the MyApp.py it seems like the code calles the function "storeString" correctly but it does not save it into the variable section (which I really need to do further string operations, for example: y=x+'hello'. I tried both adding "return x" or adding "x=storeString()" in the latter file, but it's no use in both cases. Am I missing something? – iraciv94 Jul 13 '18 at 12:34
  • You need to operate the string `x` after the user clicks the button (it makes sense, since only when the user clicks the button should the string entered in the line edit should be read in). A simple way could be to check whether the variable `x` is not an empty string (`if not (x == ``):`) and then do your operation. – mahesh Jul 13 '18 at 12:48
  • Better way would be to follow what I wrote in my code. Replace the `# Do something with the string` with your operation, or a call to the function doing the string processing. – mahesh Jul 13 '18 at 12:59