[英]Can I use a variable from a function that's inside of another class?
What I'm trying to do is create 1 window with 8 check boxes.我要做的是创建 1 个 window 和 8 个复选框。 The user will click the ones that are relevant and press "Calculate Job".
用户将单击相关的,然后按“计算工作”。 That button should then open a new window that contains buttons/sections/whatever for only the options that were selected in window 1. Sounds ok... I have window 1 set up in class First.
That button should then open a new window that contains buttons/sections/whatever for only the options that were selected in window 1. Sounds ok... I have window 1 set up in class First. When I click the button it shows up the new window, great.
当我单击按钮时,它会显示新的 window,太好了。 But now I need to find a way to say "If any of these sections are selected then do something in window 2".
但现在我需要找到一种方法来表达“如果选择了这些部分中的任何一个,那么在 window 2 中做一些事情”。
code:代码:
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_())
So what I want to do in class 'Second' is use the checkbox variable of self.data_processing to check if it has been enabled, if it has then I want to pull up a button in the Second gui window.所以我想在 class 'Second' 中做的是使用 self.data_processing 的复选框变量来检查它是否已启用,如果已启用,那么我想在 Second gui window 中拉出一个按钮。
Is what I'm trying to do possible?我正在尝试做的事情可能吗? Should I be thinking of a different way to do this?
我应该考虑另一种方法来做到这一点吗? I'd like someones opinion on this if possible, and a little bit of guidance.
如果可能的话,我希望有人对此发表意见,并提供一些指导。 I'd really appreciate the help, I've spent my whole weekend messing around with this and I'm just getting nowhere.
我真的很感激你的帮助,我整个周末都在搞这个,但我一无所获。
You need to call your second class inside first class when your checkbox is clicked单击复选框时,您需要在第一个 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_())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.