NEKO

pyqt简单实例

2017/11/27
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
# -*- coding: utf-8 -*-
"""
Created on Mon Jul 17 15:42:23 2017

@author: kuraraneko

"""

import sys
import random
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *


class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
QToolTip.setFont(QFont('sansserif',10))

self.setToolTip('this is a <b>QWidger</b> widget')

btn=QPushButton('Button',self)
btn.setToolTip('This is a <b>QPushButton</b> widget')
btn.resize(btn.sizeHint())
btn.move(50,50)

self.setGeometry(300,300,300,200)
self.setWindowTitle('Tooltips')
self.show()

#只是一个窗口
class window(QWidget):
def __init__(self):
super().__init__()
self.title='it\' s a window'
self.left=10
self.top=10
self.width=640
self.height=480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.show()

#状态栏
class statusbar(QMainWindow):
def __init__(self):
super().__init__()
self.title='it\'s a status bar'
self.left=10
self.top=10
self.width=640
self.height=480
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)
self.statusBar().showMessage('message in statusbar')
self.show()

#button
class button(QWidget):
def __init__(self):
super().__init__()
self.title='it\' a button'
self.left=800
self.top=400
self.width=320
self.height=200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)

button=QPushButton('button',self)
button.setToolTip('it\'s a example button')
button.move(100,70)
button.clicked.connect(self.on_click)

self.show()

@pyqtSlot()
def on_click(self):
print('hello')

# 对话
class Dialog(QDialog):
def slot_method(self):
print('指きりあえば,詩は続いて')
def __init__(self):
super(QDialog,self).__init__()

button=QPushButton('click')
button.clicked.connect(self.slot_method)

mainLayout=QVBoxLayout() #布局
mainLayout.addWidget(button) #添加button

self.setGeometry(800,400,320,200)
self.setWindowTitle('指切り')
self.setLayout(mainLayout) #使用此布局
self.show()

#对话框
class message(QWidget):
def __init__(self):
super().__init__()
self.title='リオ'
self.left=800
self.top=400
self.width=320
self.height=200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)

buttonreply=QMessageBox.question(self,'it\'s a message','yes or no',QMessageBox.Yes|QMessageBox.No,QMessageBox.No)
if buttonreply==QMessageBox.Yes:
print('you click yes')
else:
print('you click no')

self.show()

#文本框
class TextBox(QMainWindow):
def __init__(self):
super().__init__()
self.title='runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 200
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)

self.textbox=QLineEdit(self)
self.textbox.move(20,20)
self.textbox.resize(200,40)

self.button=QPushButton('show text',self)
self.button.move(20,80)

self.button.clicked.connect(self.on_click)
self.show()

@pyqtSlot()
def on_click(self):
textboxValue=self.textbox.text()
QMessageBox.question(self,'textbox','you typed:'+textboxValue,QMessageBox.Ok,QMessageBox.Ok)
self.text.setText('')

#设置位置
class position(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)

label1=QLabel('label1',self)
label1.move(50,50)

label2=QLabel('label2',self)
label2.move(100,100)

label3=QLabel('label3',self)
label3.move(150,150)

label4=QLabel('label4',self)
label4.move(200,200)

self.show()

#菜单栏
class menuBar(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

mainmenu=self.menuBar()
filemenu=mainmenu.addMenu('File')
editmenu=mainmenu.addMenu('Edit')
viewmenu=mainmenu.addMenu('View')
searchmenu=mainmenu.addMenu('Search')
toolsMenu=mainmenu.addMenu('Tools')
helpmenu=mainmenu.addMenu('Help')

exitButton=QAction(QIcon('1.png'),'Exit',self)
exitButton.setShortcut('Ctrl+Q')
exitButton.setStatusTip('Exit application')
exitButton.triggered.connect(self.close)
filemenu.addAction(exitButton)

self.statusBar()#显示下端状态栏,没这个setStatusTip将不会出现

self.show()

#工具栏
class toolBar(QMainWindow):
def __init__(self):
super().__init__()
self.initUi()

def initUi(self):
self.setWindowTitle('runa')
self.setGeometry(800,400,400,400)

exitaction=QAction(QIcon('1.png'),'exit',self)
exitaction.setShortcut('Ctrl+Q')
exitaction.setStatusTip('exit file')
exitaction.triggered.connect(quit)

self.statusBar()

self.toolbar=self.addToolBar('exit')
self.toolbar.addAction(exitaction)

self.show()

#表
class Table(QWidget):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.createTable()

self.layout=QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)

self.show()

def createTable(self):
self.tableWidget=QTableWidget()
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(2)
self.tableWidget.setItem(0, 0, QTableWidgetItem('Cell (1,1)'))
self.tableWidget.setItem(0, 1, QTableWidgetItem('Cell (1,2)'))
self.tableWidget.setItem(1, 0, QTableWidgetItem('Cell (2,1)'))
self.tableWidget.setItem(1, 1, QTableWidgetItem('Cell (2,2)'))
self.tableWidget.setItem(2, 0, QTableWidgetItem('Cell (3,1)'))
self.tableWidget.setItem(2, 1, QTableWidgetItem('Cell (3,2)'))
self.tableWidget.setItem(3, 0, QTableWidgetItem('Cell (4,1)'))
self.tableWidget.setItem(3, 1, QTableWidgetItem('Cell (4,2)'))
self.tableWidget.move(0,0)

self.tableWidget.doubleClicked.connect(self.on_click)

@pyqtSlot()
def on_click(self):
print('\n')
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())

#切换
class Tabs(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.table_widget=MyTableWidget(self)
self.setCentralWidget(self.table_widget)

self.show()
class MyTableWidget(QWidget):
def __init__(self, parent):
super().__init__(parent)
self.layout = QVBoxLayout(self)

# Initialize tab screen
self.tabs = QTabWidget()
self.tab1 = QWidget()
self.tab2 = QWidget()
self.tabs.resize(300, 200)

# Add tabs
self.tabs.addTab(self.tab1, "Tab 1")
self.tabs.addTab(self.tab2, "Tab 2")

# Create first tab
self.tab1.layout = QVBoxLayout(self)
self.pushButton1 = QPushButton("PyQt5 button")
self.tab1.layout.addWidget(self.pushButton1)
self.tab1.setLayout(self.tab1.layout)

# Add tabs to widget
self.layout.addWidget(self.tabs)
self.setLayout(self.layout)

#平面布置
class HorizontalLayout(QDialog):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.createHorizontalLayout()

windowLayout=QVBoxLayout()
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)

self.show()

def createHorizontalLayout(self):
self.horizontalGroupBox=QGroupBox('what\'s your favouriate color?')
layout=QHBoxLayout()

buttonBlue=QPushButton('blue',self)
buttonBlue.clicked.connect(self.on_click)
layout.addWidget(buttonBlue)

buttonRed = QPushButton('Red', self)
buttonRed.clicked.connect(self.on_click)
layout.addWidget(buttonRed)

buttonGreen = QPushButton('Green', self)
buttonGreen.clicked.connect(self.on_click)
layout.addWidget(buttonGreen)

self.horizontalGroupBox.setLayout(layout)

@pyqtSlot()
def on_click(self):
print('(⊙o⊙)')

#网格布局
class gridLayout(QDialog):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.createGridLayout()

windowLayout=QVBoxLayout()
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)

self.show()

def createGridLayout(self):
self.horizontalGroupBox=QGroupBox('Grid')
layout=QGridLayout()
#拉伸窗口组件比例放大
layout.setColumnStretch(0, 4)
layout.setColumnStretch(1, 4)
layout.setColumnStretch(2, 4)

layout.addWidget(QPushButton('1'), 0, 0)
layout.addWidget(QPushButton('2'), 0, 1)
layout.addWidget(QPushButton('3'), 0, 2)
layout.addWidget(QPushButton('4'), 1, 0)
layout.addWidget(QPushButton('5'), 1, 1)
layout.addWidget(QPushButton('6'), 1, 2)
layout.addWidget(QPushButton('7'), 2, 0)
layout.addWidget(QPushButton('8'), 2, 1)
layout.addWidget(QPushButton('9'), 2, 2)

self.horizontalGroupBox.setLayout(layout)

#输入对话
class inputDialogs(QWidget):
def __init__(self):
super().__init__()
self.title = 'runa'
self.left = 800
self.top = 400
self.width = 320
self.height = 400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.getInteger()
self.getText()
self.getDouble()
self.getChoice()

self.show()

def getInteger(self):
i,okPressed=QInputDialog.getInt(self,'Get integer','Percentage:',28,0,100,2)#默认,下限,上限,步长
if okPressed:
print(i)

def getDouble(self):
d, okPressed = QInputDialog.getDouble(self, "Get double", "Value:", 10.50, 0, 100, 10)
if okPressed:
print(d)

def getChoice(self):
items = ("Red", "Blue", "Green")
item, okPressed = QInputDialog.getItem(self, "Get item", "Color:", items, 0, False)
if okPressed and item:
print(item)

def getText(self):
text, okPressed = QInputDialog.getText(self, "Get text", "Your name:", QLineEdit.Normal, "")
if okPressed and text != '':
print(text)

#得到文件名字
class fileDialog(QWidget):
def __init__(self):
super().__init__()
self.title='runa'
self.left=800
self.top=400
self.width=320
self.height=400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)

self.openFileNameDialog()
self.openFileNamesDialog()
self.saveFileDialog()
# self.show() 感觉这个没什么用处

def openFileNameDialog(self):
options =QFileDialog.Options()
options |=QFileDialog.DontUseNativeDialog
fileName,_=QFileDialog.getOpenFileName(self,'QFileDialog.getOpenFileName()','','ALL Files (*);;Python Files(*.py);;Text Files (*.txt)',options=options)
if fileName:
print(fileName)#打印字符串

def openFileNamesDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
files, _ = QFileDialog.getOpenFileNames(self, "QFileDialog.getOpenFileNames()", "","All Files (*);;Python Files (*.py)", options=options)
if files:
print(files) #打印序列
#选中文件 再选中文件夹 未实现保存功能而是实现返回保存文件的名字
def saveFileDialog(self):
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
fileName, _ = QFileDialog.getSaveFileName(self, "QFileDialog.getSaveFileName()", "","All Files (*);;Text Files (*.txt)", options=options)
if fileName:
print(fileName)

#导入图像
class image(QWidget):
def __init__(self):
super().__init__()
self.title='runa'
self.left=800
self.top=400
self.width=320
self.height=400
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left,self.top,self.width,self.height)

label1=QLabel(self)
pixmap=QPixmap('C:/Users/kuraraneko/Pictures/Screenshots/1')
label1.setPixmap(pixmap)
self.resize(pixmap.width(),pixmap.height())

self.show()

#像素点
class pixels(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle("ルナ")
self.show()

def paintEvent(self,e):#父类的重构函数,回调函数,e是触发事件的句柄
qp = QPainter()
qp.begin(self)
self.drawPoints(qp)
qp.end()

def drawPoints(self, qp):
qp.setPen(Qt.red)
size = self.size()

for i in range(1000):
x = random.randint(1, size.width() - 1)
y = random.randint(1, size.height() - 1)
qp.drawPoint(x, y)

#调出颜色选择窗口
class colorDialog(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle("ルナ")

button=QPushButton('open color dialog',self)
button.setToolTip('Opens color dialog')
button.move(10,10)
button.clicked.connect(self.on_click)

self.show()

@pyqtSlot()
def on_click(self):
color=QColorDialog.getColor()

if color.isValid():
print(color.name())

#打印颜色
class colors(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(800, 400, 380, 280)
self.setWindowTitle("ルナ")
self.show()

#设置背景颜色
self.setAutoFillBackground(True)
p=self.palette()
p.setColor(self.backgroundRole(),Qt.white)
self.setPalette(p)

self.show()

def paintEvent(self,event):
qp=QPainter(self)

qp.setPen(Qt.black)
size=self.size()

qp.setBrush(QColor(200,0,0))#颜色
qp.drawRect(0,0,100,100)#位置大小

qp.setBrush(QColor(0,200, 0))
qp.drawRect(100, 0, 100, 100)

qp.setBrush(QColor(0, 0, 200))
qp.drawRect(200, 0, 100, 100)

for i in range(0, 30):
qp.setBrush(QColor(i * 10, 0, 0))
qp.drawRect(10 * i, 100, 10, 32)

qp.setBrush(QColor(i * 10, i * 10, 0))
qp.drawRect(10 * i, 100 + 32, 10, 32)

qp.setBrush(QColor(i * 2, i * 10, i * 1))
qp.drawRect(10 * i, 100 + 64, 10, 32)

#拖动文本
class drag(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(300, 300, 280, 170)
self.setWindowTitle("ルナ")

editBox=QLineEdit('ルナ',self)
editBox.setDragEnabled(True)
editBox.move(10,10)
editBox.resize(100,32)

button=CustomLabel('桜小路',self)
button.move(130,15)

button.setAcceptDrops(True)
self.show()
class CustomLabel(QLabel):

def __init__(self, title, parent):
super().__init__(title, parent)
self.setAcceptDrops(True)

def dragEnterEvent(self, e):
if e.mimeData().hasFormat('text/plain'):
e.accept()
else:
e.ignore()

def dropEvent(self, e):
self.setText(e.mimeData().text())

#调出字体选择窗口
class font(QWidget):
def __init__(self):
super().__init__()
self.initUI()

def initUI(self):
self.setGeometry(800, 400, 380, 280)
self.setWindowTitle("ルナ")

button=QPushButton('Font Dialog',self)
button.setToolTip('Font Dialog')
button.move(50,50)
button.clicked.connect(self.on_click)

self.show()
@pyqtSlot()
def on_click(self):
openFontDialog(self)
def openFontDialog(self):
font,ok=QFontDialog.getFont()
if ok:
print(font.toString())

#左上角小图标
class Icon(QWidget):
def __init__(self):
super().__init__()
self.initUi()

def initUi(self):
self.setWindowTitle('镜子')
self.setGeometry(800, 400, 400, 400)
self.setWindowIcon(QIcon('1.png'))

self.show()

#是否退出窗口
class exit(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.setGeometry(300, 300, 300, 220)
self.setWindowTitle('消息盒子')
self.show()

def closeEvent(self, event):

reply = QMessageBox.question(self, '信息', '确认退出吗?',
QMessageBox.Yes | QMessageBox.No, QMessageBox.No)

if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()

#窗口居中
class midBox(QWidget):
def __init__(self):
super(midBox, self).__init__()
self.initUi()

def initUi(self):
self.setGeometry(300,300,300,200)

self.center()

self.setWindowTitle('runa')
self.show()
def center(self):
qr=self.frameGeometry()
cp=QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
if __name__=='__main__':
app=QApplication(sys.argv)
a=toolBar()
sys.exit(app.exec_())

原文作者: n3k0

发表日期: November 27th 2017, 10:57:16

发出嘶吼: 没有魔夜2玩我要死了

CATALOG