python ui学习过程,使用pyqt5实现

首先安装pyqt5的包,然后打开notebook就可以编写了。当然这样编写,也可以用designer进行。
它是pyqt5-tools的一个exe软件,Anaconda3Libsite-packagespyqt5_toolsQTindesigner.exe,可以实现可视化编辑,
然后通过 pyuic5 -o 目标文件名.py 源文件名.ui 转化后,将源文件进行修改就可以实现对应界面了。
可能需要之类,还是看ma吧 :
 1 import sys
 2 from PyQt5.QtWidgets import QApplication, QMainWindow
 3 from mainWindow import *
 4 
 5 
 6 class MyWindow(QMainWindow, Ui_MainWindow):
 7     def __init__(self, parent=None):
 8         super(MyWindow, self).__init__(parent)
 9         self.setupUi(self)
10 
11 
12 if __name__ == '__main__':
13     app = QApplication(sys.argv)
14     myWin = MyWindow()
15     myWin.show()
16     sys.exit(app.exec_())
 
后面的代码是根据这里的文字写的,但是运行代码可以在退出的过程中无法响应或者出现kernel dead的情况。通过添加app.aboutToQuit.connect(app.deleteLater)在主函数中,可避免。
if __name__ == '__main__':
app = QApplication(sys.argv)
app.aboutToQuit.connect(app.deleteLater)
ex = Example()
app.exec_()

链接:https://www.jianshu.com/p/5a9bf7548fdb
来源:简书

  1 #!/usr/bin/env python
  2 # coding: utf-8
  3 
  4 # In[1]:
  5 
  6 
  7 #建立窗口
  8 import sys
  9 from PyQt5.QtWidgets import QApplication, QWidget
 10 if __name__ == '__main__':
 11     app = QApplication(sys.argv)
 12     w = QWidget()
 13     w.resize(250, 150)
 14     w.move(300, 300)
 15     w.setWindowTitle('Simple')
 16     w.show()
 17     sys.exit(app.exec_())
 18 
 19 
 20 # In[ ]:
 21 
 22 
 23 #将窗口在屏幕上显示,并设置了它的尺寸。setGeometry()方法的前两个参数定位了窗口的x轴和y轴位置。第三个参数是定义窗口的宽度,第四个参数是定义窗口的高度。
 24 #建立窗口图标
 25 import sys
 26 from PyQt5.QtWidgets import QApplication, QWidget
 27 from PyQt5.QtGui import QIcon
 28 class Example(QWidget):
 29     def __init__(self):
 30         super().__init__()
 31         self.initUI()   
 32     def initUI(self):
 33         self.setGeometry(100, 300, 300, 220)
 34         self.setWindowTitle('Icon')
 35         self.setWindowIcon(QIcon('test.png'))
 36         self.show()  
 37 if __name__ == '__main__':
 38     app = QApplication(sys.argv)
 39     ex = Example()
 40     sys.exit(app.exec_()) 
 41 
 42 
 43 # In[1]:
 44 
 45 
 46 #创建按钮和TIP指示
 47 import sys
 48 from PyQt5.QtWidgets import (QWidget, QToolTip,
 49     QPushButton, QApplication)
 50 from PyQt5.QtGui import QFont
 51 class Example(QWidget):
 52     def __init__(self):
 53         super().__init__()
 54         self.initUI()
 55     def initUI(self):
 56         QToolTip.setFont(QFont('SansSerif', 10))
 57         self.setToolTip('This is a <b>QWidget</b> widget')
 58         btn = QPushButton('Button', self)
 59         btn.setToolTip('This is a <b>QPushButton</b> widget')#使用富文本格式
 60         #btn.resize(btn.sizeHint())#创建了一个按钮组件并且为它设置一个提示框。
 61         btn.move(50, 50)      
 62         self.setGeometry(300, 300, 300, 200)
 63         self.setWindowTitle('Tooltips')   
 64         self.show()  
 65 if __name__ == '__main__':
 66     app = QApplication(sys.argv)
 67     ex = Example()
 68     sys.exit(app.exec_())
 69 
 70 
 71 # In[1]:
 72 
 73 
 74 #将按钮链接到退出方案
 75 import sys
 76 from PyQt5.QtWidgets import QWidget, QPushButton, QApplication
 77 from PyQt5.QtCore import QCoreApplication
 78 class Example(QWidget):
 79     def __init__(self):
 80         super().__init__()
 81         self.initUI()
 82     def initUI(self):              
 83         qbtn = QPushButton('Quit', self)
 84         qbtn.clicked.connect(QCoreApplication.instance().quit)
 85         qbtn.resize(qbtn.sizeHint())
 86         qbtn.move(50, 50)      
 87         self.setGeometry(300, 300, 250, 150)
 88         self.setWindowTitle('Quit button')   
 89         self.show()
 90 if __name__ == '__main__':
 91     app = QApplication(sys.argv)
 92     ex = Example()
 93     sys.exit(app.exec_())
 94 
 95 
 96 # In[1]:
 97 
 98 
 99 #退出事件的触发与取舍
100 import sys
101 from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
102 class Example(QWidget):
103     def __init__(self):
104         super().__init__()
105         self.initUI()
106     def initUI(self):              
107         self.setGeometry(300, 300, 250, 150)       
108         self.setWindowTitle('Message box')   
109         self.show()
110     def closeEvent(self, event):
111         reply = QMessageBox.question(self, 'Message',"Are you sure to quit?", QMessageBox.Yes |
112             QMessageBox.No, QMessageBox.No)
113         if reply == QMessageBox.Yes:
114             event.accept()
115         else:
116             event.ignore()       
117 if __name__ == '__main__':
118     app = QApplication(sys.argv)
119     ex = Example()
120     sys.exit(app.exec_())
121 
122 
123 # In[1]:
124 
125 
126 #居中窗口
127 import sys
128 from PyQt5.QtWidgets import QWidget, QDesktopWidget, QApplication
129 class Example(QWidget):
130     def __init__(self):
131         super().__init__()
132         self.initUI()
133     def initUI(self):              
134         self.resize(250, 150)
135         self.center()#将窗口居中放置
136         self.setWindowTitle('Center')
137         self.show()
138     def center(self):
139         qr = self.frameGeometry()#获得主窗口的一个矩形特定几何图形。这包含了窗口的框架
140         cp = QDesktopWidget().availableGeometry().center()#计算出相对于显示器的绝对值。并且从这个绝对值中,我们获得了屏幕中心点
141         qr.moveCenter(cp)#把矩形的中心设置到屏幕的中间去
142         self.move(qr.x(),qr.y())#移动了应用窗口的左上方的点到qr矩形的左上方的点,因此居中显示在我们的屏幕上
143 if __name__ == '__main__':
144     app = QApplication(sys.argv)
145     ex = Example()
146     sys.exit(app.exec_()) 
147 
148 
149 # In[1]:
150 
151 
152 #显示状态栏的信息
153 import sys
154 from PyQt5.QtWidgets import QMainWindow, QApplication
155 class Example(QMainWindow):
156     def __init__(self):
157         super().__init__() 
158         self.initUI()
159     def initUI(self):              
160         self.statusBar().showMessage('Ready')
161         self.setGeometry(300, 300, 250, 150)
162         self.setWindowTitle('Statusbar')   
163         self.show()
164 if __name__ == '__main__':
165     app = QApplication(sys.argv)
166     ex = Example()
167     sys.exit(app.exec_())
168 
169 
170 # In[1]:
171 
172 
173 #创建了一个菜单栏。我们创建一个file菜单,然后将退出动作添加到file菜单中
174 import sys
175 from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
176 from PyQt5.QtGui import QIcon
177 class Example(QMainWindow):
178     def __init__(self):
179         super().__init__()
180         self.initUI()
181     def initUI(self):              
182         exitAction = QAction(QIcon('test.jpg'), '&Exit', self)#创建标签
183         exitAction.setShortcut('Ctrl+Q')#定义了一个快捷键
184         exitAction.setStatusTip('Exit application')
185         exitAction.triggered.connect(qApp.quit)
186         self.statusBar()
187         menubar = self.menuBar()
188         fileMenu = menubar.addMenu('&File')
189         fileMenu.addAction(exitAction)
190         self.setGeometry(300, 300, 300, 200)
191         self.setWindowTitle('Menubar')   
192         self.show()    
193 if __name__ == '__main__':
194     app = QApplication(sys.argv)
195     ex = Example()
196     sys.exit(app.exec_())
197 
198 
199 # In[1]:
200 
201 
202 #创建工具栏
203 import sys
204 from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication
205 from PyQt5.QtGui import QIcon
206 class Example(QMainWindow): 
207     def __init__(self):
208         super().__init__()
209         self.initUI()
210     def initUI(self):              
211         exitAction = QAction(QIcon('test.png'), 'Exit1', self)
212         exitAction.setShortcut('Ctrl+Q')
213         exitAction1 = QAction(QIcon('test.png'), 'Exit1', self)
214         exitAction1.setShortcut('Ctrl+W')
215         exitAction.triggered.connect(qApp.quit)
216         self.toolbar = self.addToolBar('Exit')
217         self.toolbar.addAction(exitAction)
218         self.toolbar.addAction(exitAction1)
219         self.setGeometry(300, 300, 300, 200)
220         self.setWindowTitle('Toolbar')   
221         self.show()  
222 if __name__ == '__main__':
223     app = QApplication(sys.argv)
224     ex = Example()
225     sys.exit(app.exec_())
226 
227 
228 # In[1]:
229 
230 
231 #设置文本相对于窗口的位置
232 import sys
233 from PyQt5.QtWidgets import QWidget, QLabel, QApplication
234 class Example(QWidget):
235     def __init__(self):
236         super().__init__()
237         self.initUI()
238     def initUI(self):
239         lbl1 = QLabel('Zetcode', self)
240         lbl1.move(15, 10)
241         lbl2 = QLabel('tutorials', self)
242         lbl2.move(35, 40)
243         lbl3 = QLabel('for programmers', self)
244         lbl3.move(55, 70)
245         self.setGeometry(300, 300, 250, 150)
246         self.setWindowTitle('Absolute')   
247         self.show()
248 if __name__ == '__main__':
249      
250     app = QApplication(sys.argv)
251     ex = Example()
252     sys.exit(app.exec_())
253 
254 
255 # In[1]:
256 
257 
258 import sys
259 from PyQt5.QtWidgets import (QWidget, QPushButton,
260     QHBoxLayout, QVBoxLayout, QApplication)
261 class Example(QWidget):
262     def __init__(self):
263         super().__init__()
264         self.initUI()
265     def initUI(self):
266         okButton = QPushButton("OK")
267         cancelButton = QPushButton("Cancel")
268         hbox = QHBoxLayout()
269         hbox.addStretch(1)#增加了一个拉伸因子,拉伸因子在两个按钮之前增加了一个可伸缩空间。这具体不知道。
270         hbox.addWidget(okButton)
271         hbox.addWidget(cancelButton)
272         vbox = QVBoxLayout()
273         vbox.addStretch(0)#增加了一个拉伸因子,拉伸因子在两个按钮之前增加了一个可伸缩空间。这具体不知道。
274         vbox.addLayout(hbox)
275         self.setLayout(vbox)   
276          
277         self.setGeometry(300, 300, 300, 150)
278         self.setWindowTitle('Buttons')   
279         self.show()
280 if __name__ == '__main__':
281     app = QApplication(sys.argv)
282     ex = Example()
283     sys.exit(app.exec_())
284 
285 
286 # In[ ]:
287 
288 
289 #创建一个网格的定位列表,计算器样式
290 import sys
291 from PyQt5.QtWidgets import (QWidget, QGridLayout,
292     QPushButton, QApplication)
293 class Example(QWidget):
294     def __init__(self):
295         super().__init__()
296         self.initUI()
297     def initUI(self):
298         grid = QGridLayout()
299         self.setLayout(grid)
300         names = ['Cls', 'Bck', '', 'Close',
301                  '7', '8', '9', '/',
302                 '4', '5', '6', '*',
303                  '1', '2', '3', '-',
304                 '0', '.', '=', '+']
305         positions = [(i,j) for i in range(5) for j in range(4)]
306         for position, name in zip(positions, names): 
307             if name == '':
308                 continue
309             button = QPushButton(name)
310             grid.addWidget(button, *position)
311         self.move(300, 150)
312         self.setWindowTitle('Calculator')
313         self.show()
314 if __name__ == '__main__':
315     app = QApplication(sys.argv)
316     ex = Example()
317     sys.exit(app.exec_())
318 
319 
320 # In[ ]:
321 
322 
323 #文本审阅窗口,利用网格定位。
324 import sys
325 from PyQt5.QtWidgets import (QWidget, QLabel, QLineEdit,
326     QTextEdit, QGridLayout, QApplication)
327 class Example(QWidget):
328     def __init__(self):
329         super().__init__()
330         self.initUI()  
331     def initUI(self):
332         title = QLabel('Title')
333         author = QLabel('Author')
334         review = QLabel('Review')
335         titleEdit = QLineEdit()
336         authorEdit = QLineEdit()
337         reviewEdit = QTextEdit()
338         grid = QGridLayout()
339         grid.setSpacing(10)
340         grid.addWidget(title, 1, 0)
341         grid.addWidget(titleEdit, 1, 1)
342         grid.addWidget(author, 2, 0)
343         grid.addWidget(authorEdit, 2, 1)
344         grid.addWidget(review, 3, 0)
345         grid.addWidget(reviewEdit, 3, 1, 5, 1)
346         self.setLayout(grid)
347         self.setGeometry(300, 300, 350, 300)
348         self.setWindowTitle('Review')   
349         self.show() 
350 if __name__ == '__main__':
351     app = QApplication(sys.argv)
352     ex = Example()
353     sys.exit(app.exec_())
354 
355 
356 # In[ ]:
357 
358 
359 #时间和槽,拖动滑块条的把手,lcd数字会变化
360 import sys
361 from PyQt5.QtCore import Qt
362 from PyQt5.QtWidgets import (QWidget, QLCDNumber, QSlider,QVBoxLayout, QApplication)
363 class Example(QWidget):
364     def __init__(self):
365         super().__init__()
366         self.initUI()
367     def initUI(self):
368         lcd = QLCDNumber(self)
369         sld = QSlider(Qt.Horizontal, self)
370         vbox = QVBoxLayout()
371         vbox.addWidget(lcd)
372         vbox.addWidget(sld)
373         self.setLayout(vbox)
374         sld.valueChanged.connect(lcd.display)
375         self.setGeometry(300, 300, 250, 150)
376         self.setWindowTitle('Signal & slot')
377         self.show()
378 if __name__ == '__main__':
379     app = QApplication(sys.argv)
380     ex = Example()
381     sys.exit(app.exec_())
382 
383 
384 # In[1]:
385 
386 
387 #重写了keyPressEvent()事件处理函数,Esc按键按下的处理
388 import sys
389 from PyQt5.QtCore import Qt
390 from PyQt5.QtWidgets import QWidget, QApplication
391 class Example(QWidget):
392     def __init__(self):
393         super().__init__()
394         self.initUI()  
395     def initUI(self):     
396         self.setGeometry(300, 300, 250, 150)
397         self.setWindowTitle('Event handler')
398         self.show() 
399     def keyPressEvent(self, e):
400         if e.key() == Qt.Key_Escape:
401             self.close()
402 if __name__ == '__main__':
403     app = QApplication(sys.argv)
404     ex = Example()
405     sys.exit(app.exec_())
406 
407 
408 # In[ ]:
409 
410 
411 #利用sender解决事件发送者的问题
412 import sys
413 from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication
414 class Example(QMainWindow):
415     def __init__(self):
416         super().__init__()
417         self.initUI()
418     def initUI(self):     
419         btn1 = QPushButton("Button 1", self)
420         btn1.move(30, 50)
421         btn2 = QPushButton("Button 2", self)
422         btn2.move(150, 50)
423         btn1.clicked.connect(self.buttonClicked)           
424         btn2.clicked.connect(self.buttonClicked)#两个按钮都连接到了同一个槽中。
425         self.statusBar()
426         self.setGeometry(300, 300, 290, 150)
427         self.setWindowTitle('Event sender')
428         self.show()
429     def buttonClicked(self):
430         sender = self.sender()
431         self.statusBar().showMessage(sender.text() + ' was pressed')
432 if __name__ == '__main__':
433     app = QApplication(sys.argv)
434     ex = Example()
435     sys.exit(app.exec_())
436 
437 
438 # In[4]:
439 
440 
441 #发送自定义的信号
442 import sys
443 from PyQt5.QtCore import pyqtSignal, QObject
444 from PyQt5.QtWidgets import QMainWindow, QApplication
445 class Communicate(QObject):
446     closeApp = pyqtSignal()#创建一个新的信号叫做closeApp,并且成为外部类Communicate类的属性
447 
448 class Example(QMainWindow):
449     def __init__(self):
450         super().__init__()
451         self.initUI()
452     def initUI(self):     
453         self.c = Communicate()
454         self.c.closeApp.connect(self.close)#把自定义的closeApp信号连接到QMainWindow的close()槽上
455         self.setGeometry(300, 300, 290, 150)
456         self.setWindowTitle('Emit signal')
457         self.show() 
458     def mousePressEvent(self, event):
459         self.c.closeApp.emit()#closeApp信号会被发射。
460 if __name__ == '__main__':
461     app = QApplication(sys.argv)
462     app.aboutToQuit.connect(app.deleteLater)
463     ex = Example()
464     sys.exit(app.exec_())
465 
466 
467 # In[1]:
468 
469 
470 #输入对话框与显示
471 import sys
472 from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
473     QInputDialog, QApplication)
474 class Example(QWidget):
475     def __init__(self):
476         super().__init__()
477         self.initUI() 
478     def initUI(self):     
479         self.btn = QPushButton('Dialog', self)
480         self.btn.move(20, 20)
481         self.btn.clicked.connect(self.showDialog)
482         self.le = QLineEdit(self)
483         self.le.move(130, 22)
484         self.setGeometry(300, 300, 290, 150)
485         self.setWindowTitle('Input dialog')
486         self.show()
487     def showDialog(self):
488         text, ok = QInputDialog.getText(self, 'Input Dialog',
489             'Enter your name:')#第一个字符串参数是对话框的标题,第二个字符串参数是对话框内的消息文本。对话框返回输入的文本内容和一个布尔值。
490         if ok:
491             self.le.setText(str(text)) 
492 if __name__ == '__main__':
493     app = QApplication(sys.argv)
494     ex = Example()
495     sys.exit(app.exec_())
496 
497 
498 # In[ ]:
499 
500 
501 #颜色选择对话框
502 import sys
503 from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
504     QColorDialog, QApplication)
505 from PyQt5.QtGui import QColor
506 class Example(QWidget):
507     def __init__(self):
508         super().__init__()
509         self.initUI()
510     def initUI(self):     
511         col = QColor(0, 0, 0)
512         self.btn = QPushButton('Dialog', self)
513         self.btn.move(20, 20)
514         self.btn.clicked.connect(self.showDialog)
515         self.frm = QFrame(self)
516         self.frm.setStyleSheet("QWidget { background-color: %s }"
517             % col.name())
518         self.frm.setGeometry(130, 22, 100, 100)           
519         self.setGeometry(300, 300, 250, 180)
520         self.setWindowTitle('Color dialog')
521         self.show()
522     def showDialog(self):
523         col = QColorDialog.getColor()
524         if col.isValid():
525             self.frm.setStyleSheet("QWidget { background-color: %s }"
526                 % col.name())   
527 if __name__ == '__main__':
528     app = QApplication(sys.argv)
529     ex = Example()
530     sys.exit(app.exec_())
531 
532 
533 # In[ ]:
534 
535 
536 #字体选择框
537 import sys
538 from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton,
539     QSizePolicy, QLabel, QFontDialog, QApplication)
540 class Example(QWidget):
541     def __init__(self):
542         super().__init__()
543         self.initUI()
544     def initUI(self):     
545         vbox = QVBoxLayout()
546         btn = QPushButton('Dialog', self)
547         btn.setSizePolicy(QSizePolicy.Fixed,
548             QSizePolicy.Fixed)
549         btn.move(20, 20)
550         vbox.addWidget(btn)
551         btn.clicked.connect(self.showDialog)
552         self.lbl = QLabel('Knowledge only matters', self)
553         self.lbl.move(130, 20)
554         vbox.addWidget(self.lbl)
555         self.setLayout(vbox)         
556         self.setGeometry(300, 300, 250, 180)
557         self.setWindowTitle('Font dialog')
558         self.show()
559     def showDialog(self):
560         font, ok = QFontDialog.getFont()
561         if ok:
562             self.lbl.setFont(font)
563 if __name__ == '__main__':
564     app = QApplication(sys.argv)
565     ex = Example()
566     sys.exit(app.exec_())
567 
568 
569 # In[ ]:
570 
571 
572 #文件对话框,读取文件
573 import sys
574 from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
575     QAction, QFileDialog, QApplication)
576 from PyQt5.QtGui import QIcon
577 class Example(QMainWindow):
578     def __init__(self):
579         super().__init__()
580         self.initUI()
581     def initUI(self):     
582         self.textEdit = QTextEdit()
583         self.setCentralWidget(self.textEdit)
584         self.statusBar()
585         openFile = QAction(QIcon('open.png'), 'Open', self)
586         openFile.setShortcut('Ctrl+O')
587         openFile.setStatusTip('Open new File')
588         openFile.triggered.connect(self.showDialog)
589         menubar = self.menuBar()
590         fileMenu = menubar.addMenu('&File')
591         fileMenu.addAction(openFile)
592         self.setGeometry(300, 300, 350, 300)
593         self.setWindowTitle('File dialog')
594         self.show()
595     def showDialog(self):
596         fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
597         if fname[0]:
598             f = open(fname[0], 'r')
599             with f:
600                 data = f.read()
601                 self.textEdit.setText(data)       
602          
603 if __name__ == '__main__':
604     app = QApplication(sys.argv)
605     ex = Example()
606     sys.exit(app.exec_())
607 
608 
609 # In[4]:
610 
611 
612 #复选框
613 import sys
614 from PyQt5.QtWidgets import QWidget, QCheckBox, QApplication
615 from PyQt5.QtCore import Qt
616 class Example(QWidget):
617     def __init__(self):
618         super().__init__() 
619         self.initUI() 
620     def initUI(self):     
621         cb = QCheckBox('Show title', self)
622         cb.move(20, 20)
623         cb.toggle()#选中复选框
624         cb.stateChanged.connect(self.changeTitle)
625         self.setGeometry(300, 300, 250, 150)
626         self.setWindowTitle('QCheckBox')
627         self.show()
628     def changeTitle(self, state):
629         if state == Qt.Checked:
630             self.setWindowTitle('QCheckBox')
631         else:
632             self.setWindowTitle('')
633 if __name__ == '__main__':
634     app = QApplication(sys.argv)
635     app.aboutToQuit.connect(app.deleteLater)
636     ex = Example()
637     sys.exit(app.exec_())
638 
639 
640 # In[6]:
641 
642 
643 #红绿蓝按钮,切换按钮
644 import sys
645 from PyQt5.QtWidgets import (QWidget, QPushButton,
646     QFrame, QApplication)
647 from PyQt5.QtGui import QColor
648 class Example(QWidget):
649     def __init__(self):
650         super().__init__()
651         self.initUI()
652     def initUI(self):     
653         self.col = QColor(0, 0, 0)      
654         redb = QPushButton('Red', self)
655         redb.setCheckable(True)
656         redb.move(10, 10)
657         redb.clicked[bool].connect(self.setColor)
658         redb = QPushButton('Green', self)
659         redb.setCheckable(True)
660         redb.move(10, 60)
661         redb.clicked[bool].connect(self.setColor)
662         blueb = QPushButton('Blue', self)
663         blueb.setCheckable(True)
664         blueb.move(10, 110)
665         blueb.clicked[bool].connect(self.setColor)
666         self.square = QFrame(self)
667         self.square.setGeometry(150, 20, 100, 100)
668         self.square.setStyleSheet("QWidget { background-color: %s }" % 
669             self.col.name())
670         self.setGeometry(300, 300, 280, 170)
671         self.setWindowTitle('Toggle button')
672         self.show()
673     def setColor(self, pressed):
674         source = self.sender()
675         if pressed:
676             val = 255
677         else: val = 0
678         if source.text() == "Red":
679             self.col.setRed(val)               
680         elif source.text() == "Green":
681             self.col.setGreen(val)
682         else:
683             self.col.setBlue(val)
684         self.square.setStyleSheet("QFrame { background-color: %s }" %
685             self.col.name()) 
686 if __name__ == '__main__':
687     app = QApplication(sys.argv)
688     app.aboutToQuit.connect(app.deleteLater)
689     ex = Example()
690     sys.exit(app.exec_())
691 
692 
693 # In[ ]:
694 
695 
696 #根据滑动条改变图片
697 import sys
698 from PyQt5.QtWidgets import (QWidget, QSlider,
699     QLabel, QApplication)
700 from PyQt5.QtCore import Qt
701 from PyQt5.QtGui import QPixmap
702 class Example(QWidget):
703     def __init__(self):
704         super().__init__()
705         self.initUI()
706     def initUI(self):     
707         sld = QSlider(Qt.Horizontal, self)
708         sld.setFocusPolicy(Qt.NoFocus)
709         sld.setGeometry(30, 40, 100, 30)
710         sld.valueChanged[int].connect(self.changeValue)
711         self.label = QLabel(self)
712         self.label.setPixmap(QPixmap('mute.png'))
713         self.label.setGeometry(160, 40, 80, 30)
714         self.setGeometry(300, 300, 280, 170)
715         self.setWindowTitle('QSlider')
716         self.show()
717     def changeValue(self, value):
718         if value == 0:
719             self.label.setPixmap(QPixmap('mute.png'))
720         elif value > 0 and value <= 30:
721             self.label.setPixmap(QPixmap('min.png'))
722         elif value > 30 and value < 80:
723             self.label.setPixmap(QPixmap('med.png'))
724         else:
725             self.label.setPixmap(QPixmap('test.png'))
726 if __name__ == '__main__':
727     app = QApplication(sys.argv)
728     app.aboutToQuit.connect(app.deleteLater)
729     ex = Example()
730     sys.exit(app.exec_())
731 
732 
733 # In[3]:
734 
735 
736 #利用按钮控制进度条的发展
737 import sys
738 from PyQt5.QtWidgets import (QWidget, QProgressBar,
739     QPushButton, QApplication)
740 from PyQt5.QtCore import QBasicTimer
741 class Example(QWidget):
742     def __init__(self):
743         super().__init__()
744         self.initUI()
745     def initUI(self):     
746         self.pbar = QProgressBar(self)
747         self.pbar.setGeometry(30, 40, 200, 25)
748         self.btn = QPushButton('Start', self)
749         self.btn.move(40, 80)
750         self.btn.clicked.connect(self.doAction)
751         self.timer = QBasicTimer()
752         self.step = 0
753         self.setGeometry(300, 300, 280, 170)
754         self.setWindowTitle('QProgressBar')
755         self.show()
756     def timerEvent(self, e):
757         if self.step >= 100:
758             self.timer.stop()
759             self.btn.setText('Finished')
760             return
761         self.step = self.step + 1
762         self.pbar.setValue(self.step)
763     def doAction(self):
764         if self.timer.isActive():
765             self.timer.stop()
766             self.btn.setText('Start')
767         else:
768             self.timer.start(100, self)
769             self.btn.setText('Stop')
770 if __name__ == '__main__':
771     app = QApplication(sys.argv)
772     app.aboutToQuit.connect(app.deleteLater)
773     ex = Example()
774     sys.exit(app.exec_())
775 
776 
777 # In[4]:
778 
779 
780 #日历组件
781 import sys
782 from PyQt5.QtWidgets import (QWidget, QCalendarWidget,
783     QLabel, QApplication)
784 from PyQt5.QtCore import QDate
785 class Example(QWidget):
786     def __init__(self):
787         super().__init__()
788         self.initUI()
789     def initUI(self):     
790         cal = QCalendarWidget(self)
791         cal.setGridVisible(True)
792         cal.move(20, 20)
793         cal.clicked[QDate].connect(self.showDate)
794         self.lbl = QLabel(self)
795         date = cal.selectedDate()
796         self.lbl.setText(date.toString())
797         self.lbl.move(130, 260)
798         self.setGeometry(300, 300, 350, 300)
799         self.setWindowTitle('Calendar')
800         self.show()
801     def showDate(self, date):    
802         self.lbl.setText(date.toString()) 
803 if __name__ == '__main__':
804     app = QApplication(sys.argv)
805     app.aboutToQuit.connect(app.deleteLater)
806     ex = Example()
807     sys.exit(app.exec_())
808 
809 
810 # In[6]:
811 
812 
813 #添加像素图
814 import sys
815 from PyQt5.QtWidgets import (QWidget, QHBoxLayout,
816     QLabel, QApplication)
817 from PyQt5.QtGui import QPixmap
818 class Example(QWidget):
819     def __init__(self):
820         super().__init__()
821         self.initUI()
822     def initUI(self):     
823         hbox = QHBoxLayout(self)
824         pixmap = QPixmap("test.jpg")
825         lbl = QLabel(self)
826         lbl.setPixmap(pixmap)
827         hbox.addWidget(lbl)
828         self.setLayout(hbox)
829         self.move(300, 200)
830         self.setWindowTitle('Red Rock')
831         self.show()
832 if __name__ == '__main__':
833     app = QApplication(sys.argv)
834     app.aboutToQuit.connect(app.deleteLater)
835     ex = Example()
836     sys.exit(app.exec_())
837 
838 
839 # In[ ]:
840 
841 
842 #单行文本编辑和标签一起变化
843 import sys
844 from PyQt5.QtWidgets import (QWidget, QLabel,
845     QLineEdit, QApplication)
846 class Example(QWidget):
847     def __init__(self):
848         super().__init__()
849         self.initUI()
850     def initUI(self):     
851         self.lbl = QLabel(self)
852         qle = QLineEdit(self)
853         qle.move(60, 100)
854         self.lbl.move(60, 40)
855         qle.textChanged[str].connect(self.onChanged)
856         self.setGeometry(300, 300, 280, 170)
857         self.setWindowTitle('QLineEdit')
858         self.show()
859     def onChanged(self, text):
860         self.lbl.setText(text)
861         self.lbl.adjustSize()
862 if __name__ == '__main__':
863     app = QApplication(sys.argv)
864     ex = Example()
865     sys.exit(app.exec_())
866 
867 
868 # In[3]:
869 
870 
871 #分割框,水平,垂直,嵌套
872 import sys
873 from PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,
874     QSplitter, QStyleFactory, QApplication)
875 from PyQt5.QtCore import Qt
876 class Example(QWidget):
877     def __init__(self):
878         super().__init__()
879         self.initUI()
880     def initUI(self):     
881         hbox = QHBoxLayout(self)
882         topleft = QFrame(self)
883         topleft.setFrameShape(QFrame.StyledPanel)
884         topright = QFrame(self)
885         topright.setFrameShape(QFrame.StyledPanel)
886         bottom = QFrame(self)
887         bottom.setFrameShape(QFrame.StyledPanel)
888         splitter1 = QSplitter(Qt.Horizontal)
889         splitter1.addWidget(topleft)
890         splitter1.addWidget(topright)
891         splitter2 = QSplitter(Qt.Vertical)
892         splitter2.addWidget(splitter1)
893         splitter2.addWidget(bottom)
894         hbox.addWidget(splitter2)
895         self.setLayout(hbox)
896         self.setGeometry(300, 300, 300, 200)
897         self.setWindowTitle('QSplitter')
898         self.show()
899     def onChanged(self, text):
900         self.lbl.setText(text)
901         self.lbl.adjustSize()       
902 if __name__ == '__main__':
903     app = QApplication(sys.argv)
904     app.aboutToQuit.connect(app.deleteLater)
905     ex = Example()
906     sys.exit(app.exec_())
907 
908 
909 # In[5]:
910 
911 
912 #下拉列表框
913 import sys
914 from PyQt5.QtWidgets import (QWidget, QLabel,
915     QComboBox, QApplication)
916 class Example(QWidget):
917     def __init__(self):
918         super().__init__()
919         self.initUI()
920     def initUI(self):
921         self.lbl = QLabel("Ubuntu", self)
922         combo = QComboBox(self)
923         combo.addItem("Ubuntu")
924         combo.addItem("Mandriva")
925         combo.addItem("Fedora")
926         combo.addItem("Arch")
927         combo.addItem("Gentoo")
928         combo.move(50, 50)
929         self.lbl.move(50, 150)
930         combo.activated[str].connect(self.onActivated)       
931         self.setGeometry(300, 300, 300, 200)
932         self.setWindowTitle('QComboBox')
933         self.show()
934     def onActivated(self, text):
935         self.lbl.setText(text)
936         self.lbl.adjustSize()
937 if __name__ == '__main__':
938     app = QApplication(sys.argv)
939     app.aboutToQuit.connect(app.deleteLater)
940     ex = Example()
941     sys.exit(app.exec_())
942 
943 
944 # In[ ]:
View Code

代码在上,包含了几乎所有的那个啥了。如果需要,那就有看看吧。

原文地址:https://www.cnblogs.com/bai2018/p/10539947.html