【问题标题】:Can I use a variable from a function that's inside of another class?我可以使用另一个类内部的函数中的变量吗?
【发布时间】:2020-12-21 09:05:42
【问题描述】:

我要做的是创建 1 个带有 8 个复选框的窗口。用户将单击相关的,然后按“计算工作”。然后,该按钮应打开一个新窗口,其中包含按钮/部分/仅适用于在窗口 1 中选择的选项。听起来不错...我在 First 类中设置了窗口 1。当我单击按钮时,它会显示新窗口,太棒了!但现在我需要找到一种方式来表达“如果选择了这些部分中的任何一个,则在窗口 2 中执行某些操作”。

代码:

from PyQt5 import QtWidgets
import sys
import qdarkstyle

class First(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.title = 'Job Price Calculator'
        self.left = 100
        self.top = 100
        self.width = 320
        self.height = 350
        self.initUI()
        self.dialog = Second()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.data_processing = QtWidgets.QCheckBox('Data Processing', self)
        self.data_processing.move(50, 50)

        self.digital_print = QtWidgets.QCheckBox('Digital Print', self)
        self.digital_print.move(50, 100)

        self.calculate = QtWidgets.QPushButton('Calculate Job', self)
        self.calculate.move(100, 250)
        self.calculate.clicked.connect(self.on_button_click)

    def on_button_click(self):
        self.dialog.show()

class Second(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        self.title = 'Job Specification'
        self.left = 500
        self.top = 100
        self.width = 1080
        self.height = 920
        self.initUI_specification()

    def initUI_specification(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())

        if self.data_processing.isChecked():
            self.data_processing_info()

        if self.digital_print.isChecked():
            self.digital_print_info()

    def data_processing_info(self):
        self.temp_button = QtWidgets.QPushButton('Temp Button', self)
        self.temp_button.move(100, 250)

    def digital_print_info(self):
        self.temp_button2 = QtWidgets.QPushButton('Temp Button 2', self)
        self.temp_button2.move(100, 450)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())

所以我想在“第二”类中做的是使用 self.data_processing 的复选框变量来检查它是否已启用,如果已启用,那么我想在第二个 gui 窗口中拉出一个按钮。

我正在尝试做的事情可能吗?我应该考虑另一种方法来做到这一点吗?如果可能的话,我希望有人对此发表意见,并提供一些指导。我真的很感谢你的帮助,我整个周末都在搞这个,但我一无所获。

【问题讨论】:

  • self.dialog = Second - def on_button_click(self): self.dialog(param 1, param2, param3, param4).show() - 然后用 class Second(..): def __init__(self, param1, param2, param3, param4, parent=None): 中的 param_n_ 做点什么?
  • 或做一个def on_button_click(self): self.dialog.setup(param1,param2, ...); self.dialog.show() 来设置你的第二个对话框选项。

标签: python class user-interface pyqt5 qcheckbox


【解决方案1】:

当您的复选框被点击时,您需要在第一堂课中调用您的第二堂课

from PyQt5 import QtWidgets
import sys
import qdarkstyle

class First(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(First, self).__init__(parent)
        self.title = 'Job Price Calculator'
        self.left = 100
        self.top = 100
        self.width = 320
        self.height = 350
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.data_processing = QtWidgets.QCheckBox('Data Processing', self)
        self.data_processing.move(50, 50)
        self.data_processing.stateChanged.connect(self.checkbox_clicked)

        self.digital_print = QtWidgets.QCheckBox('Digital Print', self)
        self.digital_print.move(50, 100)

        self.calculate = QtWidgets.QPushButton('Calculate Job', self)
        self.calculate.move(100, 250)
        self.calculate.clicked.connect(self.on_button_click)

        self.secondClass = Second()

    def on_button_click(self):
        self.dialog.show()
    
    def checkbox_clicked(self):
        if self.data_processing.isChecked():
            self.secondClass.data_processing_info()
            self.secondClass.show()
        else:
            self.secondClass.hide() #hide or close - as your requirement

class Second(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(Second, self).__init__(parent)
        self.title = 'Job Specification'
        self.left = 500
        self.top = 100
        self.width = 1080
        self.height = 920
        self.initUI_specification()

    def initUI_specification(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)
        self.setStyleSheet(qdarkstyle.load_stylesheet_pyqt5())


    def data_processing_info(self):
        self.temp_button = QtWidgets.QPushButton('Temp Button', self)
        self.temp_button.move(100, 250)

    def digital_print_info(self):
        self.temp_button2 = QtWidgets.QPushButton('Temp Button 2', self)
        self.temp_button2.move(100, 450)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    main = First()
    main.show()
    sys.exit(app.exec_())

【讨论】:

  • 我明白了。但是当我点击“数据处理”框时,它现在会立即打开一个新窗口。我需要做的是点击所有我想要的,然后当我点击计算按钮将打开包含这些部分的新窗口......这有意义吗?
  • 是的,你可以这样做。
  • 我不明白如何使用它来勾选多个复选框。假设用户勾选了 8 个复选框中的 6 个,我如何在第二个窗口中显示 6 个不同的功能?如果我勾选两个框,则以下代码不起作用: def checkbox_clicked(self): if self.data_processing.isChecked(): self.secondClass = Second() self.secondClass.data_processing_info() elif self.digital_print.isChecked() : self.secondClass = Second() self.secondClass.digital_print_info()
  • 您可以根据需要进行更改。我建议您只创建一个函数并将对象名称作为参数传递。因此,每当单击任何复选框时,相同的函数都会具有单击复选框的对象,并且可用于更新您的变量
猜你喜欢
  • 1970-01-01
  • 2014-07-31
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-30
相关资源
最近更新 更多