i try to use value from txtbox in Window1 in other Class by click button in Window1 i tried more than 100 ways but still not wroking! how can i get values of txtbox in Window1 when i click button and get run callstock and in callstock first i get value of textbox from Window1 note : code works fine when i set value "example" but i cant get that value from class Window1
class Window1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(550,200,500,500)
self.setWindowTitle('program')
self.UI()
def UI(self):
self.txtbox = QLineEdit(self)
self.txtbox.move(150,10)
self.txtbox.setPlaceholderText("name")
btnfind = QPushButton("Find",self)
btnfind.move(175,35)
btnfind.clicked.connect(self.getValues)
self.show()
def sendval(self):
txt = self.txtbox.text()
return txt
def getValues(self):
self.newB = callstock()
class callstock(QWidget):
def __init__(self):
super().__init__()
wi = Window1.sendval(self)
self.setWindowTitle(wi)
self.setGeometry(10,10,500,500)
self.UI()
self.figview1 = showchart1(name=wi,day=200)
self.figview1.setGeometry(10,10,500,500)
self.figview1.move(0,0)
self.figview1.show()
def UI(self):
pass
def main():
app = QApplication(sys.argv)
window = Window1()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
CodePudding user response:
You have to send instance of first class to second class as argument - in your code it will be self
which means instance of Window1
self.newB = callstock(self)
And second class has to get it - ie. as other_class
class callstock(QWidget):
def __init__(self, other_class): # <-- get instance in `other_class`
super().__init__()
wi = other_class.txtbox.text() # <-- use it
Minimal working code:
from PyQt5.Qt import *
class Window1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(550, 200, 500, 500) # PEP8: space after comma
self.setWindowTitle('program')
self.UI()
def UI(self):
self.txtbox = QLineEdit(self)
self.txtbox.move(150, 10) # PEP8: space after comma
self.txtbox.setPlaceholderText("name")
btnfind = QPushButton(self, text="Find") # PEP8: space after comma
btnfind.move(175, 35) # PEP8: space after comma
btnfind.clicked.connect(self.getValues)
self.show()
def getValues(self):
self.newB = CallStock(self) # <-- send instance of `Window1` as argument
class CallStock(QWidget): # PEP8: `CamelCaseName` for classes
def __init__(self, other_class): # <-- get instance in variable `other_class`
super().__init__()
wi = other_class.txtbox.text() # <-- use it
self.setWindowTitle(wi)
self.setGeometry(10, 10, 500, 500) # PEP8: space after comma
self.UI()
self.figview1 = QLabel(self, text="Text from Window1: " wi)
#self.figview1 = showchart1(name=wi, day=200) # PEP8: space after comma
self.figview1.setGeometry(10, 10, 500, 500) # PEP8: space after comma
self.figview1.move(0, 0) # PEP8: space after comma
self.figview1.show()
self.show()
def UI(self):
pass
def main():
app = QApplication([])
window = Window1()
app.exec()
if __name__ == '__main__':
main()
PEP 8 -- Style Guide for Python Code
EDIT:
You could also send only text instead of full instance
self.newB = CallStock(self.txtbox.text())
and
class CallStock(QWidget): # PEP8: `CamelCaseName` for classes
def __init__(self, wi): # <-- get text in variable `wi`
Minimal working code:
from PyQt5.Qt import *
class Window1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(550, 200, 500, 500) # PEP8: space after comma
self.setWindowTitle('program')
self.UI()
def UI(self):
self.txtbox = QLineEdit(self)
self.txtbox.move(150, 10) # PEP8: space after comma
self.txtbox.setPlaceholderText("name")
btnfind = QPushButton(self, text="Find") # PEP8: space after comma
btnfind.move(175, 35) # PEP8: space after comma
btnfind.clicked.connect(self.getValues)
self.show()
def getValues(self):
self.newB = CallStock(self.txtbox.text())
class CallStock(QWidget): # PEP8: `CamelCaseName` for classes
def __init__(self, wi):
super().__init__()
self.setWindowTitle(wi)
self.setGeometry(10, 10, 500, 500) # PEP8: space after comma
self.UI()
self.figview1 = QLabel(self, text="Text from Window1: " wi)
#self.figview1 = showchart1(name=wi, day=200) # PEP8: space after comma
self.figview1.setGeometry(10, 10, 500, 500) # PEP8: space after comma
self.figview1.move(0, 0) # PEP8: space after comma
self.figview1.show()
self.show()
def UI(self):
pass
def main():
app = QApplication([])
window = Window1()
app.exec()
if __name__ == '__main__':
main()