ฉันมี 2 radiobuttons กสร้างขึ้น(ใน QMainWindow เรียน)อย่าง:
def dtype_radiobuttons(self):
layout = QHBoxLayout()
rb1 = QRadioButton("complex")
rb1.toggled.connect(lambda: self.update_image("dtype", rb1.text()))
self.real_dtype_rb = QRadioButton("real", self)
self.real_dtype_rb.toggled.connect(lambda: self.update_image("dtype", self.real_dtype_rb.text()))
self.btngroup.append(QButtonGroup())
self.btngroup[-1].addButton(self.real_dtype_rb)
self.btngroup[-1].addButton(rb1)
rb1.setChecked(True)
layout.addWidget(rb1)
layout.addWidget(self.real_dtype_rb)
layout.addStretch()
return layout
def library_radiobutton(self):
layout = QHBoxLayout()
self.cvnn_library_rb = QRadioButton("cvnn", self)
self.cvnn_library_rb.toggled.connect(lambda: self.update_image("library", self.cvnn_library_rb.text()))
rb2 = QRadioButton("tensorflow", self)
rb2.toggled.connect(lambda: self.update_image("library", rb2.text()))
self.btngroup.append(QButtonGroup())
self.btngroup[-1].addButton(rb2)
self.btngroup[-1].addButton(self.cvnn_library_rb)
self.cvnn_library_rb.setChecked(True)
layout.addWidget(self.cvnn_library_rb)
layout.addWidget(rb2)
layout.addStretch()
return layout
ฉันอยากจะทำให้มัน เป็นไปไม่ได้ ที่จะเลือก complex
ตัวเลือกของ dtype radiobuttons กลุ่มแล้ว tensorflow
radiobutton ของห้องสมุด radiobuttons. ไป 3 จาก 4 เป็นไปได้ combinations. ดังนั้นถ้าฉันเลือก complex
และห้องสมุดเป็น tensorflow
ฉันอยากจะเปลี่ยนโดยอัตโนมัติที่ห้องสมุดเพื่อไป cvnn
. ฉันพยายามด้วยอะไรซักอย่างมันเหมือนนี้:
def update_image(self, key, value):
if value == "complex":
if hasattr(self, 'cvnn_library_rb'): # It wont exists if I still didnt create the radiobutton.
self.cvnn_library_rb.setChecked(True) # Set library cvnn
elif value == "tensorflow":
if hasattr(self, 'real_dtype_rb'):
self.real_dtype_rb.setChecked(True) # Set real dtype
... Do the other stuff I need to do.
สิ่งที่แปลกคือว่าจริงๆแล้วมันทำงานอยู่ในความรู้สึกว่าตัวอย่างเช่นถ้าผมเป็นอยู่ complex
เปิดใช้งานและเลือก tensorflow
ที่ radiobutton การเปลี่ยนแปลงไป real
(สิ่งที่ฉันต้องการ!) แต่ tensorflow
ยังไม่ได้เลือก! ฉันต้องการเพื่อเลือกมันอีกครั้งเหมือนว่าจะทำ self.real_dtype_rb.setChecked(True)
cancels ส่วนที่เลือกไว้ของ radiobutton ฉันมีการคลิกบน (แปลกมากถ้าคุณถามฉัน).
คน
hasattr
คือชินเพราะขึ้นอยู่กับลำดับของฉันโทรหา ฟังก์ชันมีบางอย่า radiobuttons นั้นจะถูกสร้างขึ้นก่อน คนอื่นดังนั้นมันอาจจะไม่มีอยู่จริงนี่ เป็นทางเลือกฉันพิจารณาแต่มันกำลังปิดการใช้งานที่ radiobutton กลุ่มแทนที่จะเปลี่ยนของรัฐ(ไม่ใช่สิ่งที่ฉันชอบ).