学pyqt5之前需要学python吗_python ui学习过程,使用pyqt5实现

1 #!/usr/bin/env python

2 #coding: utf-8

3

4 #In[1]:

5

6

7 #建立窗口

8 importsys9 from PyQt5.QtWidgets importQApplication, QWidget10 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 importsys26 from PyQt5.QtWidgets importQApplication, QWidget27 from PyQt5.QtGui importQIcon28 classExample(QWidget):29 def __init__(self):30 super().__init__()31 self.initUI()32 definitUI(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 importsys48 from PyQt5.QtWidgets import(QWidget, QToolTip,49 QPushButton, QApplication)50 from PyQt5.QtGui importQFont51 classExample(QWidget):52 def __init__(self):53 super().__init__()54 self.initUI()55 definitUI(self):56 QToolTip.setFont(QFont('SansSerif', 10))57 self.setToolTip('This is a QWidget widget')58 btn = QPushButton('Button', self)59 btn.setToolTip('This is a QPushButton 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 importsys76 from PyQt5.QtWidgets importQWidget, QPushButton, QApplication77 from PyQt5.QtCore importQCoreApplication78 classExample(QWidget):79 def __init__(self):80 super().__init__()81 self.initUI()82 definitUI(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 importsys101 from PyQt5.QtWidgets importQWidget, QMessageBox, QApplication102 classExample(QWidget):103 def __init__(self):104 super().__init__()105 self.initUI()106 definitUI(self):107 self.setGeometry(300, 300, 250, 150)108 self.setWindowTitle('Message box')109 self.show()110 defcloseEvent(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 importsys128 from PyQt5.QtWidgets importQWidget, QDesktopWidget, QApplication129 classExample(QWidget):130 def __init__(self):131 super().__init__()132 self.initUI()133 definitUI(self):134 self.resize(250, 150)135 self.center()#将窗口居中放置

136 self.setWindowTitle('Center')137 self.show()138 defcenter(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 importsys154 from PyQt5.QtWidgets importQMainWindow, QApplication155 classExample(QMainWindow):156 def __init__(self):157 super().__init__()158 self.initUI()159 definitUI(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 importsys175 from PyQt5.QtWidgets importQMainWindow, QAction, qApp, QApplication176 from PyQt5.QtGui importQIcon177 classExample(QMainWindow):178 def __init__(self):179 super().__init__()180 self.initUI()181 definitUI(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 importsys204 from PyQt5.QtWidgets importQMainWindow, QAction, qApp, QApplication205 from PyQt5.QtGui importQIcon206 classExample(QMainWindow):207 def __init__(self):208 super().__init__()209 self.initUI()210 definitUI(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 importsys233 from PyQt5.QtWidgets importQWidget, QLabel, QApplication234 classExample(QWidget):235 def __init__(self):236 super().__init__()237 self.initUI()238 definitUI(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 importsys259 from PyQt5.QtWidgets import(QWidget, QPushButton,260 QHBoxLayout, QVBoxLayout, QApplication)261 classExample(QWidget):262 def __init__(self):263 super().__init__()264 self.initUI()265 definitUI(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 importsys291 from PyQt5.QtWidgets import(QWidget, QGridLayout,292 QPushButton, QApplication)293 classExample(QWidget):294 def __init__(self):295 super().__init__()296 self.initUI()297 definitUI(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 inzip(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 importsys325 from PyQt5.QtWidgets import(QWidget, QLabel, QLineEdit,326 QTextEdit, QGridLayout, QApplication)327 classExample(QWidget):328 def __init__(self):329 super().__init__()330 self.initUI()331 definitUI(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 importsys361 from PyQt5.QtCore importQt362 from PyQt5.QtWidgets import(QWidget, QLCDNumber, QSlider,QVBoxLayout, QApplication)363 classExample(QWidget):364 def __init__(self):365 super().__init__()366 self.initUI()367 definitUI(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 importsys389 from PyQt5.QtCore importQt390 from PyQt5.QtWidgets importQWidget, QApplication391 classExample(QWidget):392 def __init__(self):393 super().__init__()394 self.initUI()395 definitUI(self):396 self.setGeometry(300, 300, 250, 150)397 self.setWindowTitle('Event handler')398 self.show()399 defkeyPressEvent(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 importsys413 from PyQt5.QtWidgets importQMainWindow, QPushButton, QApplication414 classExample(QMainWindow):415 def __init__(self):416 super().__init__()417 self.initUI()418 definitUI(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 defbuttonClicked(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 importsys443 from PyQt5.QtCore importpyqtSignal, QObject444 from PyQt5.QtWidgets importQMainWindow, QApplication445 classCommunicate(QObject):446 closeApp = pyqtSignal()#创建一个新的信号叫做closeApp,并且成为外部类Communicate类的属性

447

448 classExample(QMainWindow):449 def __init__(self):450 super().__init__()451 self.initUI()452 definitUI(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 defmousePressEvent(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 importsys472 from PyQt5.QtWidgets import(QWidget, QPushButton, QLineEdit,473 QInputDialog, QApplication)474 classExample(QWidget):475 def __init__(self):476 super().__init__()477 self.initUI()478 definitUI(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 defshowDialog(self):488 text, ok = QInputDialog.getText(self, 'Input Dialog',489 'Enter your name:')#第一个字符串参数是对话框的标题,第二个字符串参数是对话框内的消息文本。对话框返回输入的文本内容和一个布尔值。

490 ifok: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 importsys503 from PyQt5.QtWidgets import(QWidget, QPushButton, QFrame,504 QColorDialog, QApplication)505 from PyQt5.QtGui importQColor506 classExample(QWidget):507 def __init__(self):508 super().__init__()509 self.initUI()510 definitUI(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 defshowDialog(self):523 col =QColorDialog.getColor()524 ifcol.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 importsys538 from PyQt5.QtWidgets import(QWidget, QVBoxLayout, QPushButton,539 QSizePolicy, QLabel, QFontDialog, QApplication)540 classExample(QWidget):541 def __init__(self):542 super().__init__()543 self.initUI()544 definitUI(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 defshowDialog(self):560 font, ok =QFontDialog.getFont()561 ifok: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 importsys574 from PyQt5.QtWidgets import(QMainWindow, QTextEdit,575 QAction, QFileDialog, QApplication)576 from PyQt5.QtGui importQIcon577 classExample(QMainWindow):578 def __init__(self):579 super().__init__()580 self.initUI()581 definitUI(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 defshowDialog(self):596 fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')597 iffname[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 importsys614 from PyQt5.QtWidgets importQWidget, QCheckBox, QApplication615 from PyQt5.QtCore importQt616 classExample(QWidget):617 def __init__(self):618 super().__init__()619 self.initUI()620 definitUI(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 defchangeTitle(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 importsys645 from PyQt5.QtWidgets import(QWidget, QPushButton,646 QFrame, QApplication)647 from PyQt5.QtGui importQColor648 classExample(QWidget):649 def __init__(self):650 super().__init__()651 self.initUI()652 definitUI(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 defsetColor(self, pressed):674 source =self.sender()675 ifpressed:676 val = 255

677 else: val =0678 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 importsys698 from PyQt5.QtWidgets import(QWidget, QSlider,699 QLabel, QApplication)700 from PyQt5.QtCore importQt701 from PyQt5.QtGui importQPixmap702 classExample(QWidget):703 def __init__(self):704 super().__init__()705 self.initUI()706 definitUI(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 defchangeValue(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 importsys738 from PyQt5.QtWidgets import(QWidget, QProgressBar,739 QPushButton, QApplication)740 from PyQt5.QtCore importQBasicTimer741 classExample(QWidget):742 def __init__(self):743 super().__init__()744 self.initUI()745 definitUI(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 =0753 self.setGeometry(300, 300, 280, 170)754 self.setWindowTitle('QProgressBar')755 self.show()756 deftimerEvent(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 defdoAction(self):764 ifself.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 importsys782 from PyQt5.QtWidgets import(QWidget, QCalendarWidget,783 QLabel, QApplication)784 from PyQt5.QtCore importQDate785 classExample(QWidget):786 def __init__(self):787 super().__init__()788 self.initUI()789 definitUI(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 defshowDate(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 importsys815 from PyQt5.QtWidgets import(QWidget, QHBoxLayout,816 QLabel, QApplication)817 from PyQt5.QtGui importQPixmap818 classExample(QWidget):819 def __init__(self):820 super().__init__()821 self.initUI()822 definitUI(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 importsys844 from PyQt5.QtWidgets import(QWidget, QLabel,845 QLineEdit, QApplication)846 classExample(QWidget):847 def __init__(self):848 super().__init__()849 self.initUI()850 definitUI(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 defonChanged(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 importsys873 from PyQt5.QtWidgets import(QWidget, QHBoxLayout, QFrame,874 QSplitter, QStyleFactory, QApplication)875 from PyQt5.QtCore importQt876 classExample(QWidget):877 def __init__(self):878 super().__init__()879 self.initUI()880 definitUI(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 defonChanged(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 importsys914 from PyQt5.QtWidgets import(QWidget, QLabel,915 QComboBox, QApplication)916 classExample(QWidget):917 def __init__(self):918 super().__init__()919 self.initUI()920 definitUI(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 defonActivated(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[ ]:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值