我可以使用另一个 class 中的函数中的变量吗?
Can I use a variable from a function that's inside of another class?
我想要做的是创建 1 个 window 和 8 个复选框。用户将单击相关的那些并按“计算作业”。然后该按钮应打开一个新的 window,其中仅包含 window 1 中选择的选项的 buttons/sections/whatever。听起来不错...我已在 window 1 中设置class第一。当我单击按钮时,它会显示新的 window,太棒了!但是现在我需要找到一种方法来表达“如果选择了这些部分中的任何一个,那么在 window 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_())
所以我想在class 'Second'中做的是使用self.data_processing的复选框变量来检查它是否已启用,如果已启用则我想拉起第二个 gui 中的一个按钮 window.
我想做的事情可行吗?我应该考虑一种不同的方法来做到这一点吗?如果可能的话,我希望有人对此发表意见,并提供一些指导。非常感谢您的帮助,我整个周末都在搞这个,但我一无所获。
您需要先在 class 中调用您的第二个 class 当您的复选框被点击时
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_())
我想要做的是创建 1 个 window 和 8 个复选框。用户将单击相关的那些并按“计算作业”。然后该按钮应打开一个新的 window,其中仅包含 window 1 中选择的选项的 buttons/sections/whatever。听起来不错...我已在 window 1 中设置class第一。当我单击按钮时,它会显示新的 window,太棒了!但是现在我需要找到一种方法来表达“如果选择了这些部分中的任何一个,那么在 window 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_())
所以我想在class 'Second'中做的是使用self.data_processing的复选框变量来检查它是否已启用,如果已启用则我想拉起第二个 gui 中的一个按钮 window.
我想做的事情可行吗?我应该考虑一种不同的方法来做到这一点吗?如果可能的话,我希望有人对此发表意见,并提供一些指导。非常感谢您的帮助,我整个周末都在搞这个,但我一无所获。
您需要先在 class 中调用您的第二个 class 当您的复选框被点击时
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_())