【问题标题】:Cant Get values from other class in Python无法从 Python 中的其他类获取值
【发布时间】:2022-01-27 02:38:22
【问题描述】:

我尝试在其他类的 Window1 中使用 txtbox 中的值 通过单击 Window1 中的按钮 我尝试了 100 多种方法,但仍然不行! 当我单击按钮时,如何在 Window1 中获取 txtbox 的值 运行 callstock 并首先在 callstock 中从 Window1 获取文本框的值 注意:当我设置值“示例”时,代码工作正常 但我无法从 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()

【问题讨论】:

  • 您应该将第一类(或textbox)作为参数发送到第二类 - self.newB = callstock(self) 其中self 表示Window1 的实例 - 或self.newB = callstock(self.textbox) - 稍后在callstock()您必须将其分配给变量def __init__(self, other_class):def __init__(self, other_textbox): - 稍后您可以使用w1 = other_class.sendval()w1 = other_class..txtbox.text()w1 = other_textbox.text()。您不能在其他班级使用Window1 访问头等舱。
  • 您可以在代码中添加所有import 以生成最少的工作代码,我们可以简单地复制这些代码来创建解决方案。

标签: python python-3.x pyqt pyqt5


【解决方案1】:

您必须将第一类的实例作为参数发送到第二类 - 在您的代码中它将是 self,这意味着 Window1 的实例

   self.newB = callstock(self)

二等舱必须得到它 - 即。如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()

【讨论】:

  • 我的问题解决了很多
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 2015-05-10
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多