本文通过python3+pyqt5实现自定义部件–counters自定 窗口部件。这个窗口是3*3的网格。本文有两个例子如下:
/home/yrd/eric_workspace/chap11/counters.py。
/home/yrd/eric_workspace/chap11/counters_dnd.py
第二个例子在第一个例子的基础上实现能通过鼠标拖拽球到不同的网格中。
/home/yrd/eric_workspace/chap11/counters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
#!/usr/bin/env python3
from pyqt5.qtcore import (qrectf, qsize, qt)
from pyqt5.qtwidgets import (qapplication, qsizepolicy,qwidget)
from pyqt5.qtgui import qpainter,qpen
blank, red, yellow = range ( 3 )
class counterswidget(qwidget):
def __init__( self , parent = none):
super (counterswidget, self ).__init__(parent)
self .setsizepolicy(qsizepolicy(qsizepolicy.expanding,
qsizepolicy.expanding))
self .grid = [[blank] * 3 for i in range ( 3 )]
self .selected = [ 0 , 0 ]
self .setminimumsize( self .minimumsizehint())
def sizehint( self ):
return qsize( 200 , 200 )
def minimumsizehint( self ):
return qsize( 100 , 100 )
def mousepressevent( self , event):
xoffset = self .width() / 3
yoffset = self .height() / 3
if event.x() < xoffset:
x = 0
elif event.x() < 2 * xoffset:
x = 1
else :
x = 2
if event.y() < yoffset:
y = 0
elif event.y() < 2 * yoffset:
y = 1
else :
y = 2
cell = self .grid[x][y]
if cell = = blank:
cell = red
elif cell = = red:
cell = yellow
else :
cell = blank
self .grid[x][y] = cell
self .selected = [x, y]
self .update()
def keypressevent( self , event):
if event.key() = = qt.key_left:
self .selected[ 0 ] = ( 2 if self .selected[ 0 ] = = 0
else self .selected[ 0 ] - 1 )
elif event.key() = = qt.key_right:
self .selected[ 0 ] = ( 0 if self .selected[ 0 ] = = 2
else self .selected[ 0 ] + 1 )
elif event.key() = = qt.key_up:
self .selected[ 1 ] = ( 2 if self .selected[ 1 ] = = 0
else self .selected[ 1 ] - 1 )
elif event.key() = = qt.key_down:
self .selected[ 1 ] = ( 0 if self .selected[ 1 ] = = 2
else self .selected[ 1 ] + 1 )
elif event.key() = = qt.key_space:
x, y = self .selected
cell = self .grid[x][y]
if cell = = blank:
cell = red
elif cell = = red:
cell = yellow
else :
cell = blank
self .grid[x][y] = cell
self .update()
def paintevent( self , event = none):
painter = qpainter( self )
painter.setrenderhint(qpainter.antialiasing, true)
xoffset = self .width() / 3
yoffset = self .height() / 3
for x in range ( 3 ):
for y in range ( 3 ):
cell = self .grid[x][y]
rect = (qrectf(x * xoffset, y * yoffset,
xoffset, yoffset).adjusted( 0.5 , 0.5 , - 0.5 , - 0.5 ))
color = none
if cell = = red:
color = qt.red
elif cell = = yellow:
color = qt.yellow
if color is not none:
painter.save()
painter.setpen(qt.black)
painter.setbrush(color)
painter.drawellipse(rect.adjusted( 2 , 2 , - 2 , - 2 ))
painter.restore()
if [x, y] = = self .selected:
painter.setpen(qpen(qt.blue, 3 ))
else :
painter.setpen(qt.black)
painter.drawrect(rect)
if __name__ = = "__main__" :
import sys
app = qapplication(sys.argv)
form = counterswidget()
form.setwindowtitle( "counters" )
form.show()
app.exec_()
|
/home/yrd/eric_workspace/chap11/counters_dnd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
#!/usr/bin/env python3
from pyqt5.qtcore import (qrectf, qsize, qt)
from pyqt5.qtwidgets import (qapplication, qsizepolicy,qwidget)
from pyqt5.qtgui import qpainter,qpen,qpixmap,qcursor
blank, red, yellow = range ( 3 )
class counterswidget(qwidget):
def __init__( self , parent = none):
super (counterswidget, self ).__init__(parent)
self .setsizepolicy(qsizepolicy(qsizepolicy.expanding,
qsizepolicy.expanding))
self .grid = [[blank] * 3 for i in range ( 3 )]
self .selected = [ 0 , 0 ]
self .setminimumsize( self .minimumsizehint())
def sizehint( self ):
return qsize( 200 , 200 )
def minimumsizehint( self ):
return qsize( 100 , 100 )
def _xfromeventx( self , event):
xoffset = self .width() / 3
if event.x() < xoffset:
x = 0
elif event.x() < 2 * xoffset:
x = 1
else :
x = 2
return x
def _yfromeventy( self , event):
yoffset = self .width() / 3
if event.y() < yoffset:
y = 0
elif event.y() < 2 * yoffset:
y = 1
else :
y = 2
return y
def mousedoubleclickevent( self , event):
x = self ._xfromeventx(event)
y = self ._yfromeventy(event)
cell = self .grid[x][y]
if cell = = blank:
cell = red
elif cell = = red:
cell = yellow
else :
cell = blank
self .grid[x][y] = cell
self .selected = [x, y]
self .update()
def keypressevent( self , event):
if event.key() = = qt.key_left:
self .selected[ 0 ] = ( 2 if self .selected[ 0 ] = = 0
else self .selected[ 0 ] - 1 )
elif event.key() = = qt.key_right:
self .selected[ 0 ] = ( 0 if self .selected[ 0 ] = = 2
else self .selected[ 0 ] + 1 )
elif event.key() = = qt.key_up:
self .selected[ 1 ] = ( 2 if self .selected[ 1 ] = = 0
else self .selected[ 1 ] - 1 )
elif event.key() = = qt.key_down:
self .selected[ 1 ] = ( 0 if self .selected[ 1 ] = = 2
else self .selected[ 1 ] + 1 )
elif event.key() = = qt.key_space:
x, y = self .selected
cell = self .grid[x][y]
if cell = = blank:
cell = red
elif cell = = red:
cell = yellow
else :
cell = blank
self .grid[x][y] = cell
self .update()
def paintevent( self , event = none):
painter = qpainter( self )
painter.setrenderhint(qpainter.antialiasing, true)
xoffset = self .width() / 3
yoffset = self .height() / 3
for x in range ( 3 ):
for y in range ( 3 ):
cell = self .grid[x][y]
rect = (qrectf(x * xoffset, y * yoffset,
xoffset, yoffset).adjusted( 0.5 , 0.5 , - 0.5 , - 0.5 ))
color = none
if cell = = red:
color = qt.red
elif cell = = yellow:
color = qt.yellow
if color is not none:
painter.save()
painter.setpen(qt.black)
painter.setbrush(color)
painter.drawellipse(rect.adjusted( 2 , 2 , - 2 , - 2 ))
painter.restore()
if [x, y] = = self .selected:
painter.setpen(qpen(qt.blue, 3 ))
else :
painter.setpen(qt.black)
painter.drawrect(rect)
def mousepressevent( self , event):
self .x = self ._xfromeventx(event)
self .y = self ._yfromeventy(event)
cell = self .grid[ self .x][ self .y]
color = qt.darkgray
if cell = = red:
color = qt.red
elif cell = = yellow:
color = qt.yellow
pixmap = qpixmap( 12 , 12 )
pixmap.fill(color)
self .setcursor(qcursor(pixmap))
def mousereleaseevent( self , event):
x = self ._xfromeventx(event)
y = self ._yfromeventy(event)
if self .x ! = x or self .y ! = y:
cell = self .grid[ self .x][ self .y]
self .grid[ self .x][ self .y] = blank
self .grid[x][y] = cell
self .selected = [x, y]
self .update()
self .setcursor(qt.arrowcursor)
if __name__ = = "__main__" :
import sys
app = qapplication(sys.argv)
form = counterswidget()
form.setwindowtitle( "counters" )
form.show()
app.exec_()
|
运行结果:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/xiaoyangyang20/article/details/55803064