Reputation: 67
I'm getting to know PyQt5. I am trying to pass a string entered into a textbox to the Python file to work with. The string is saved in b1_output and supposed to be changed and printed after the window is closed. However I don't know how to access it. During the first print() instance in my code it will print the old value. At the second instance it won't print at all. Thanks for your help!
My Code:
from PyQt5.QtWidgets import *
import sys
class MyWindow(QMainWindow):
b1_output = 'example'
def __init__(self):
QMainWindow.__init__(self)
self.win_width = 750
self.win_heigth = 150
self.leftmargin = 50
self.topmargin = 50
#self.width = 300
#self.height = 400
self.title = 'Title'
self.initUI()
def initUI(self):
# create window
self.setGeometry(50,50,self.win_width,self.win_heigth)
self.setWindowTitle(self.title)
self.label = QLabel(self)
# create textbox
self.textbox = QLineEdit(self)
self.textbox.resize((self.win_width-self.label.width()-150),20)
self.textbox.move(self.label.width()+100, 50)
# create button
self.b1 = QPushButton(self)
self.b1.setText('Go do magic!')
self.b1.move(self.win_width-self.b1.width()-50,100)
self.b1.clicked.connect(self.clicked)
def clicked(self):
self.b1_output = self.textbox.text()
self.update()
self.close()
def update(self):
self.label.adjustSize()
app = QApplication(sys.argv)
win = MyWindow()
print(win.b1_output)
win.show()
sys.exit(app.exec_())
print(win.b1_output)
Upvotes: 0
Views: 102
Reputation: 244132
The goal of sys.exit() is to terminate the program so all code after that line will never be executed. The solution is to use sys.exit() after printing:
app = QApplication(sys.argv)
win = MyWindow()
print(win.b1_output)
win.show()
ret = app.exec_()
print(win.b1_output)
sys.exit(ret)
Upvotes: 2