我倾向于使用Python和PyQT,而我倾向于想做出一些东西。 以下代码读取屏幕上的像素,并在GUI中显示。但颜色是扭曲的。你能告诉我这是什么问题吗?如果我想创建一个不断更新GUI上鼠标位置的Qthread,你还可以给我一个启动点吗?示例代码很棒,不需要完整只需要让我开始使用QThread。谢谢你的帮助。
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import SIGNAL
import ctypes
from ctypes import c_int32, c_uint32, c_void_p, c_wchar_p, pointer, POINTER
GetDC = ctypes.windll.User32.GetDC
GetDC.argtypes = [c_void_p]
GetDC.restype = c_void_p
ReleaseDC = ctypes.windll.User32.ReleaseDC
ReleaseDC.argtypes = [c_void_p, c_void_p] #hwnd, hdc
ReleaseDC.restype = c_int32
GetPixel = ctypes.windll.Gdi32.GetPixel
GetPixel.argtypes = [c_void_p, c_int32, c_int32] # hdc, x, y
GetPixel.restype = c_uint32 # colorref
MAXWIDTH = 100
MAXHIGH = 28
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_ColorPicker(object):
def setupUi(self, ColorPicker):
ColorPicker.setObjectName(_fromUtf8("ColorPicker"))
ColorPicker.resize(618, 310)
self.PixelArray = []
self.cbAlwaysOnTop = QtGui.QCheckBox(ColorPicker)
self.cbAlwaysOnTop.setGeometry(QtCore.QRect(10, 10, 91, 17))
self.cbAlwaysOnTop.setObjectName(_fromUtf8("cbAlwaysOnTop"))
self.cbAlwaysOnTop.connect(self.cbAlwaysOnTop,SIGNAL("toggled(bool)"), self.AlwaysOnTop)
# Creating the Pixel Array
PixelDisplayY = 35
for i in range(0, MAXHIGH - 1):
PixelDisplayX = 10
self.PixelArray.append([])
for j in range(0, MAXWIDTH - 1):
#pixel = QtGui.QFrame(ColorPicker)
pixel = ScreenPixel(ColorPicker)
pixel.setGeometry(QtCore.QRect(PixelDisplayX, PixelDisplayY, 5, 5))
pixel.setFrameShape(QtGui.QFrame.StyledPanel)
pixel.setFrameShadow(QtGui.QFrame.Raised)
pixel.setObjectName(_fromUtf8("frame"))
pixel.setAutoFillBackground(True)
pal = QtGui.QPalette(pixel.palette())
pal.setColor(QtGui.QPalette.Window,QtGui.QColor('green'))
#pal.setColor(QtGui.QPalette.Window,QtGui.QColor(cc.rgbResult))
pixel.setPalette(pal)
self.PixelArray[i].append(pixel)
PixelDisplayX = PixelDisplayX + 6
PixelDisplayY = PixelDisplayY + 6
#pal = QtGui.QPalette(pixel.palette())
#pal.setColor(QtGui.QPalette.Window,QtGui.QColor('white'))
#self.PixelArray[1][2].setPalette(pal)
self.edResult = QtGui.QPlainTextEdit(ColorPicker)
self.edResult.setGeometry(QtCore.QRect(10, 230, 600, 71))
self.edResult.setObjectName(_fromUtf8("plainTextEdit"))
self.pbStart = QtGui.QPushButton(ColorPicker)
self.pbStart.setGeometry(QtCore.QRect(10, 200, 75, 23))
self.pbStart.setObjectName(_fromUtf8("pbStart"))
self.pbStop = QtGui.QPushButton(ColorPicker)
self.pbStop.setGeometry(QtCore.QRect(90, 200, 75, 23))
self.pbStop.setObjectName(_fromUtf8("pbStart_2"))
self.pbAll = QtGui.QPushButton(ColorPicker)
self.pbAll.setGeometry(QtCore.QRect(170, 200, 75, 23))
self.pbAll.setObjectName(_fromUtf8("pbStart_3"))
self.label = QtGui.QLabel(ColorPicker)
self.label.setGeometry(QtCore.QRect(260, 200, 61, 16))
font = QtGui.QFont()
font.setPointSize(9)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setObjectName(_fromUtf8("label"))
self.edPosX = QtGui.QLineEdit(ColorPicker)
self.edPosX.setGeometry(QtCore.QRect(330, 200, 60, 20))
self.edPosX.setObjectName(_fromUtf8("lineEdit"))
self.edPosX.setText("0")
self.edPosX.connect(self.edPosX,SIGNAL("editingFinished()"), self.UpdatePixelArray)
self.edPosY = QtGui.QLineEdit(ColorPicker)
self.edPosY.setGeometry(QtCore.QRect(400, 200, 60, 20))
self.edPosY.setObjectName(_fromUtf8("lineEdit_2"))
self.edPosY.setText("0")
self.edPosY.connect(self.edPosY,SIGNAL("editingFinished()"), self.UpdatePixelArray)
self.edPosW = QtGui.QLineEdit(ColorPicker)
self.edPosW.setGeometry(QtCore.QRect(470, 200, 60, 20))
self.edPosW.setObjectName(_fromUtf8("lineEdit_3"))
self.edPosW.setText("100")
self.edPosW.connect(self.edPosW,SIGNAL("editingFinished()"), self.UpdatePixelArray)
self.edPosH = QtGui.QLineEdit(ColorPicker)
self.edPosH.setGeometry(QtCore.QRect(540, 200, 60, 20))
self.edPosH.setObjectName(_fromUtf8("lineEdit_4"))
self.edPosH.setText("28")
self.edPosH.connect(self.edPosH,SIGNAL("editingFinished()"), self.UpdatePixelArray)
self.retranslateUi(ColorPicker)
QtCore.QMetaObject.connectSlotsByName(ColorPicker)
def retranslateUi(self, ColorPicker):
ColorPicker.setWindowTitle(_translate("ColorPicker", "Color Picker", None))
self.cbAlwaysOnTop.setText(_translate("ColorPicker", "Always On Top", None))
self.pbStart.setText(_translate("ColorPicker", "Start", None))
self.pbStop.setText(_translate("ColorPicker", "Stop", None))
self.pbAll.setText(_translate("ColorPicker", "All", None))
self.label.setText(_translate("ColorPicker", "(X,Y,W,H)", None))
def AlwaysOnTop(self):
if self.cbAlwaysOnTop.isChecked():
ColorPicker.setWindowFlags(QtCore.Qt.WindowStaysOnTopHint)
ColorPicker.show()
else:
ColorPicker.setWindowFlags(ColorPicker.windowFlags() & ~ QtCore.Qt.WindowStaysOnTopHint)
ColorPicker.show()
def __getPixelColor(self, x, y):
hdc = GetDC(0)
val = GetPixel(hdc, x, y)
ReleaseDC(0, hdc)
return val
def UpdatePixelArray(self):
x = int(self.edPosX.text())
y = int(self.edPosY.text())
w = int(self.edPosW.text())
h = int(self.edPosH.text())
cc = CHOOSECOLOR()
if int(x) < 0:
x = 0
self.edPosX.setText("0")
if int(y) < 0:
y = 0
self.edPosY.setText("0")
if int(w) > MAXWIDTH:
w = MAXWIDTH
self.edPosW.setText("100")
if int(h) > MAXHIGH:
h = MAXHIGH
self.edPosH.setText("28")
for i in range(0, MAXHIGH - 1):
for j in range(0, MAXWIDTH - 1):
pixel = self.PixelArray[i][j]
pal = QtGui.QPalette(pixel.palette())
pal.setColor(QtGui.QPalette.Window,QtGui.QColor('green'))
self.PixelArray[i][j].setPalette(pal)
self.PixelArray[i][j].staticColor = False
for i in range(0, h - 1):
for j in range(0, w - 1):
cc.rgbResult = self.__getPixelColor(j+x, i+y)
pixel = self.PixelArray[i][j]
pal = QtGui.QPalette(pixel.palette())
pal.setColor(QtGui.QPalette.Window,QtGui.QColor(cc.rgbResult))
self.PixelArray[i][j].setPalette(pal)
self.PixelArray[i][j].cc = cc
self.PixelArray[i][j].posX = j + x
self.PixelArray[i][j].posY = i + y
class CHOOSECOLOR(ctypes.Structure):
_fields_ = [('lStructSize', c_uint32),
('hwndOwner', c_void_p),
('hInstance', c_void_p),
('rgbResult', c_uint32),
('lpCustColors',POINTER(c_uint32)),
('Flags', c_uint32),
('lCustData', c_void_p),
('lpfnHook', c_void_p),
('lpTemplateName', c_wchar_p)]
class ScreenPixel(QtGui.QFrame):
def __init__(self, parent=None):
self.cc = CHOOSECOLOR
self.posX = 0
self.posY = 0
self.staticColor = False
return super().__init__(parent)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ColorPicker = QtGui.QWidget()
ui = Ui_ColorPicker()
ui.setupUi(ColorPicker)
ColorPicker.show()
sys.exit(app.exec_())