Skip to content

Instantly share code, notes, and snippets.

@cumulus13
Created July 5, 2025 06:14
Show Gist options
  • Save cumulus13/2be651d47b1dbbadb87c5e5d00177691 to your computer and use it in GitHub Desktop.
Save cumulus13/2be651d47b1dbbadb87c5e5d00177691 to your computer and use it in GitHub Desktop.
Color dialog + Color Picker
import sys
#import os
from PyQt5 import QtWidgets, QtCore, QtGui
from PyQt5.QtCore import Qt
import clipboard
class ColorDialog(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
self.clipboard_timer = QtCore.QTimer(self)
self.clipboard_timer.timeout.connect(self.check_clipboard)
self.clipboard_timer.start(1000) # Check clipboard every second
def initUI(self):
self.setWindowTitle('Color Picker')
self.setWindowIcon(QtGui.QIcon(r'c:\TOOLS\pyx\Paint.png')) # Update icon path if needed
self.setFixedSize(300, 300)
# Center the window on the primary screen initially
self.center()
# Main layout with proper margins and spacing
main_layout = QtWidgets.QVBoxLayout()
main_layout.setContentsMargins(20, 20, 20, 20) # Add margins
main_layout.setSpacing(15) # Add spacing between elements
self.setLayout(main_layout)
# Add stretch at top to center content vertically
main_layout.addStretch(1)
# Color Preview - centered
self.color_widget = QtWidgets.QLabel(self)
self.color_widget.setStyleSheet("background-color: black; border: 1px solid #ccc;")
self.color_widget.setFixedSize(120, 80)
main_layout.addWidget(self.color_widget, alignment=QtCore.Qt.AlignCenter)
# Color Name Label - centered
color_layout = QtWidgets.QHBoxLayout()
color_layout.addStretch(1) # Add stretch before
color_name_label = QtWidgets.QLabel("Color name:", self)
color_layout.addWidget(color_name_label)
self.color_name_display = QtWidgets.QLabel("BLACK", self)
self.color_name_display.setFont(QtGui.QFont("Arial", 10, QtGui.QFont.Bold))
color_layout.addWidget(self.color_name_display)
color_layout.addStretch(1) # Add stretch after
main_layout.addLayout(color_layout)
# Buttons - centered
button_layout = QtWidgets.QHBoxLayout()
button_layout.setSpacing(10)
choose_button = QtWidgets.QPushButton('Choose Color', self)
choose_button.clicked.connect(self.showDialog)
choose_button.setMinimumHeight(30)
button_layout.addWidget(choose_button)
set_button = QtWidgets.QPushButton('Set Color', self)
set_button.clicked.connect(self.setColor)
set_button.setMinimumHeight(30)
button_layout.addWidget(set_button)
main_layout.addLayout(button_layout)
# Hex Input - centered
hex_layout = QtWidgets.QHBoxLayout()
hex_layout.addStretch(1)
self.hex_input = QtWidgets.QLineEdit(self)
self.hex_input.setText("#000000")
self.hex_input.setMaximumWidth(150)
self.hex_input.setMinimumHeight(25)
self.hex_input.setAlignment(QtCore.Qt.AlignCenter)
hex_layout.addWidget(self.hex_input)
hex_layout.addStretch(1)
main_layout.addLayout(hex_layout)
# Add stretch at bottom to center content vertically
main_layout.addStretch(1)
def showDialog(self):
color = QtWidgets.QColorDialog.getColor()
if color.isValid():
self.set_color(color)
def setColor(self):
hex_text = self.hex_input.text()
try:
color = QtGui.QColor(hex_text)
if color.isValid():
self.set_color(color)
except ValueError:
QtWidgets.QMessageBox.warning(self, "Invalid Color", "Please enter a valid hex color code.")
def keyPressEvent(self, event):
if event.key() == Qt.Key_Q or event.key() == Qt.Key_Escape:
self.close()
elif event.key() == Qt.Key_A and event.modifiers() & Qt.ShiftModifier:
self.setWindowFlags(Qt.Window)
self.show()
self.center_on_current_screen() # Re-center after changing flags
elif event.key() == Qt.Key_A:
self.setWindowFlags(Qt.Window | Qt.WindowStaysOnTopHint)
self.show()
self.center_on_current_screen() # Re-center after changing flags
def set_color(self, color):
self.color_widget.setStyleSheet(f"background-color: {color.name()}")
clipboard.copy(color.name().upper())
self.color_name_display.setText(color.name().upper())
def center(self):
"""Center the window on the primary screen"""
screen = QtWidgets.QApplication.primaryScreen()
if screen:
geometry = screen.availableGeometry()
self.move(
geometry.x() + (geometry.width() - self.width()) // 2,
geometry.y() + (geometry.height() - self.height()) // 2
)
def center_on_current_screen(self):
"""Center the window on the screen where it's currently located"""
# Get the screen that contains the center of the window
window_center = self.geometry().center()
screen = QtWidgets.QApplication.screenAt(window_center)
# If no screen found, use the screen containing the window's top-left corner
if screen is None:
screen = QtWidgets.QApplication.screenAt(self.pos())
# If still no screen found, use primary screen
if screen is None:
screen = QtWidgets.QApplication.primaryScreen()
if screen:
geometry = screen.availableGeometry()
self.move(
geometry.x() + (geometry.width() - self.width()) // 2,
geometry.y() + (geometry.height() - self.height()) // 2
)
def moveEvent(self, event):
"""Called when window is moved - can be used for additional positioning logic"""
super().moveEvent(event)
# Uncomment the line below if you want the window to auto-center when moved
# QtCore.QTimer.singleShot(100, self.center_on_current_screen)
def check_clipboard(self):
clipboard_text = clipboard.paste()
if clipboard_text.startswith('#'):
try:
color = QtGui.QColor(clipboard_text)
if color.isValid():
self.set_color(color)
except ValueError:
pass
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
dialog = ColorDialog()
dialog.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment