无法从 Python 中的其他 class 获取值
Cant Get values from other class in Python
我尝试在其他 Class 中使用 Window1 中 txtbox 的值
通过单击 Window1 中的按钮
我试了100多种方法还是不行!
当我单击按钮和时,如何在 Window1 中获取 txtbox 的值
获取 运行 callstock,在 callstock 中,我首先从 Window1 获取文本框的值
注意:当我设置值“example”时代码工作正常
但我无法从 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()
您必须将第一个 class 的实例作为参数发送到第二个 class - 在您的代码中它将是 self
,这意味着 Window1
[=20 的实例=]
self.newB = callstock(self)
第二个 class 必须得到它 - 即。作为 other_class
class callstock(QWidget):
def __init__(self, other_class): # <-- get instance in `other_class`
super().__init__()
wi = other_class.txtbox.text() # <-- use it
最小工作代码:
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
编辑:
您也可以只发送文本而不是完整实例
self.newB = CallStock(self.txtbox.text())
和
class CallStock(QWidget): # PEP8: `CamelCaseName` for classes
def __init__(self, wi): # <-- get text in variable `wi`
最小工作代码:
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()
我尝试在其他 Class 中使用 Window1 中 txtbox 的值 通过单击 Window1 中的按钮 我试了100多种方法还是不行! 当我单击按钮和时,如何在 Window1 中获取 txtbox 的值 获取 运行 callstock,在 callstock 中,我首先从 Window1 获取文本框的值 注意:当我设置值“example”时代码工作正常 但我无法从 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()
您必须将第一个 class 的实例作为参数发送到第二个 class - 在您的代码中它将是 self
,这意味着 Window1
[=20 的实例=]
self.newB = callstock(self)
第二个 class 必须得到它 - 即。作为 other_class
class callstock(QWidget):
def __init__(self, other_class): # <-- get instance in `other_class`
super().__init__()
wi = other_class.txtbox.text() # <-- use it
最小工作代码:
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
编辑:
您也可以只发送文本而不是完整实例
self.newB = CallStock(self.txtbox.text())
和
class CallStock(QWidget): # PEP8: `CamelCaseName` for classes
def __init__(self, wi): # <-- get text in variable `wi`
最小工作代码:
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()