class SkinColorDialogUI(QWidget): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) # 初始化颜色 self.color = QColor(*settings.SKIN_COLOR) # 创建颜色对话框--但不显示 # self.color_dialog = QColorDialog(self.color, self.main_window) self.color_dialog = QColorDialog(self.color, self) # 创建调色板 self.palette = QPalette() self.options() def update_color(self, event: QColor) -> None: """ 更新颜色 两种方式同时 :param event: :return: """ # print(event, type(event)) # print(self.color_dialog.selectedColor(), type(self.color_dialog.selectedColor())) # QPalette.Background 表示设置背景色 # QPalette.WindowText 表示设置文本颜色 self.palette.setColor(QPalette.Background, event) # 给调色板设置颜色 self.palette.setBrush(QPalette.Base, QBrush(QColor(*event.getRgb()))) # self.palette.setColor(QPalette.Background, self.color_dialog.selectedColor()) # 给调色板设置颜色 # 给控件设置颜色 if event.isValid(): # self.main_window.setStyleSheet('QWidget {background-color:%s}' % event.name()) # self.main_window.setPalette(self.palette) self.color_dialog.setStyleSheet('QWidget {background-color:%s}' % event.name()) self.color_dialog.setPalette(self.palette) # 发色信号 communicate.skin_color.emit(event) def options(self) -> None: """ 参数设置 """ # 颜色选取信号 # 会向槽函数传递一个值---QColor # self.color_dialog.colorSelected.connect(self.update_color) # 选中最终颜色时发出信号-点击对话框的确定按钮时 self.color_dialog.currentColorChanged.connect( self.update_color) # 当前颜色变化时 # self.cd.setOption(QColorDialog.NoButtons) #选项控制--单个选项 # QColorDialog.NoButtons 不显示“ 确定”和“ 取消”按钮。(对“实时对话框”有用) # QColorDialog.ShowAlphaChannel 对话框中增加alpha设置项 self.color_dialog.setOptions( QColorDialog.NoButtons | QColorDialog.ShowAlphaChannel) # 选项控制--多个选项 # 打开对话框,会等待完成 # noinspection PyCallByClass # self.color_dialog.getColor(self.color, None) # 最终回调函数 self.color_dialog.finished.connect(self.finished_color) def setup_ui(self) -> None: # 打开对话框,等待打开 # self.color_dialog.open() self.color_dialog.show() def finished_color(self, event: int) -> None: """ 设置颜色到全局变量 :param event: :return: """ if event: pass color = self.color_dialog.currentColor().getRgb()[:3] settings.SKIN_COLOR = color
class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowFlags( #stay on top so it always appears visibly when we active with keyboard shortcut QtCore.Qt.WindowStaysOnTopHint | #we want to have minimal top bar so it looks clean, but still be resizable so not a frameless window QtCore.Qt.CustomizeWindowHint) #initialise starting state self.showing = False self.dialog = False self.loadDefaultColor() #set window color, position and size self.setStyleSheet("background-color: " + self.color.name() + ";") self.setWindowOpacity(self.color.alpha() / 255) self.oldPos = self.pos() self.setGeometry(0, 0, 500, 500) self.center() #need to show then hide otherwise program just ends if we do nothing self.show() self.hide() #when keyboard shortcut is pressed, show/hide accordingly def on_activate(self): if self.showing: self.hide() else: self.show() self.showing = not self.showing #just puts the window in the centre of the screen def center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) #called when a colour is selected in the color dialog def updateColor(self, color): self.show() if color.isValid(): self.setStyleSheet("background-color: " + color.name() + ";") self.setWindowOpacity(color.alpha() / 255) self.color = color self.saveDefaultColor() #saves color to settings.txt so it can be restored when program is next ran def saveDefaultColor(self): with open("settings.txt", "w") as f: s = ",".join([ str(self.color.alpha()), str(self.color.red()), str(self.color.green()), str(self.color.blue()) ]) f.write(s) #load most recent colour from last time def loadDefaultColor(self): with open("settings.txt", "r") as f: s = f.read().split(",") self.color = QtGui.QColor(int(s[1]), int(s[2]), int(s[3])) self.color.setAlpha(int(s[0])) #when color dialog is rejected (cancel is pressed) #need to still reshow the overlay def dialogClosed(self): self.dialog.done(0) self.show() self.showing = True #when we click on the overlay def mousePressEvent(self, event): #if left, we are dragging and moving the overlay if event.button() == Qt.LeftButton: self.oldPos = event.globalPos() #if right, we open color dialog elif event.button() == Qt.RightButton: self.hide() showing = False #initialise and show color dialog, with current colour as default self.dialog = QColorDialog(self) self.dialog.setStyleSheet("background-color: #dddddd;") self.dialog.setOption(QColorDialog.ShowAlphaChannel) self.dialog.reject = self.dialogClosed self.dialog.setCurrentColor(self.color) self.dialog.colorSelected.connect(self.updateColor) self.dialog.show() #when mouse is moved, we move overlay accordingly def mouseMoveEvent(self, event): delta = QPoint(event.globalPos() - self.oldPos) self.move(self.x() + delta.x(), self.y() + delta.y()) self.oldPos = event.globalPos()