import sys from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.setup_ui() def setup_ui(self): self.setWindowTitle("Qt桌面应用程序") self.resize(500, 300) self.color = QColor(170, 170, 255, 255) self.color_palette = QPalette() self.label = QLabel(self) self.label.setAutoFillBackground(True) self.label.setText("选择颜色") self.label.resize(500, 200) self.push_button = QPushButton(self) self.push_button.setText("show按钮") self.push_button.move(30, 250) self.push_button_2 = QPushButton(self) self.push_button_2.setText("getColor按钮") self.push_button_2.move(130, 250) def push_button_clicked(): self.dialogue_func() # self.color_dialogue.show() def open_slot(): print("open_slot", self.color_dialogue.selectedColor().getRgb()) self.color_dialogue.open(open_slot) self.dialogue_activities() self.push_button.clicked.connect(push_button_clicked) def push_button_2_clicked(): self.color_dialogue = QColorDialog(self) self.color_result = self.color_dialogue.getColor(self.color, self, "颜色选择", QColorDialog.ShowAlphaChannel) self.label.setText(f"最终颜色:{self.color_result.getRgb()}") self.push_button_2.clicked.connect(push_button_2_clicked) def dialogue_func(self): self.color_dialogue = QColorDialog(self) self.color_dialogue.setWindowTitle("颜色选择") self.color_dialogue.resize(400, 200) # self.dialogue.setModal(True) # self.color_dialogue.setWindowModality(Qt.WindowModal) self.color_dialogue.setSizeGripEnabled(True) # self.color_dialogue.setCurrentColor(self.color) # self.color_dialogue.setOptions(QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) self.color_dialogue.setCustomColor(2, self.color) def dialogue_activities(self): self.color_dialogue.accepted.connect(lambda: self.label.setText( f"接受 结果:{self.color_dialogue.result()} 最终颜色:{self.color_dialogue.selectedColor().getRgb()}")) self.color_dialogue.rejected.connect(lambda: self.label.setText( f"拒绝 结果:{self.color_dialogue.result()} 当前颜色:{self.color_dialogue.currentColor().getRgb()}")) def current_color_changed(color): self.color_palette.setColor(QPalette.Background, color) self.label.setPalette(self.color_palette) self.color_dialogue.currentColorChanged.connect(current_color_changed) if __name__ == '__main__': app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec_())