Python GUI學習筆記(二)：使用PyQt5
By: Yanwei Liu
Tags: GUI, Python, Programming, Windows 10

安裝

pip install PyQt5 pip install PyQt5-tools

找到QtDesigner

將exe程式傳送到桌面建立捷徑

載入.UI檔

from PyQt5 import QtWidgets, uic



import sys



app = QtWidgets.QApplication([])



win = uic.loadUi("mydesign.ui") #specify the location of your .ui file



win.show()



sys.exit(app.exec())

將.ui轉成.py檔案

pyuic5 mydesign.ui -o mydesign.py #轉成mydesign.py

建立視窗

from PyQt5 import QtWidgets



from mydesign import Ui_MainWindow # importing our generated file



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec())

改變字體

from PyQt5 import QtWidgets, QtGui



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.label.setFont(QtGui.QFont('SansSerif', 30)) # change font type and size





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec())

改變視窗尺寸

from PyQt5 import QtWidgets, QtGui,QtCore



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.label.setFont(QtGui.QFont('SansSerif', 30))



self.ui.label.setGeometry(QtCore.QRect(10, 10, 200, 200)) # change label geometry





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec())

改變文字

from PyQt5 import QtWidgets, QtGui,QtCore



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.label.setFont(QtGui.QFont('SansSerif', 30))



self.ui.label.setGeometry(QtCore.QRect(10, 10, 200, 200))



self.ui.label.setText("ABC123") #change label text





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec())

建立QLineEdit

from PyQt5 import QtWidgets,QtCore



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.lineEdit.setText("Welcome to LikeGeeks website") #change text



self.ui.lineEdit_2.setMaxLength(10) #set maximum length



self.ui.lineEdit_3.setEchoMode(QtWidgets.QLineEdit.Password) # password input



self.ui.lineEdit_4.setReadOnly(True) #QLineEdit readonly



self.ui.lineEdit_5.setStyleSheet("color: rgb(28, 43, 255);") #change text color



self.ui.lineEdit_6.setStyleSheet("background-color: rgb(28, 43, 255);") #change QLineEdit background color





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec())

建立按鈕

from PyQt5 import QtWidgets



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.pushButton.clicked.connect(self.btnClicked) # connecting the clicked signal with btnClicked slot



def btnClicked(self):



self.ui.label.setText("Button Clicked")





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec())

使用事件

def crackit():



print(“hazelnut cracked!”)



hazelnut = nut()



hazelnut.cracked.connect(crackit) # connecting the cracked signal with crackit slot



hazelnut.crack()

QComboBox widget

from PyQt5 import QtWidgets



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.comboBox.addItem("First item") #add item



self.ui.comboBox.addItem("Second item")





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec()) #Get all items

self.ui.comboBox.setCurrentIndex(1) #select by index which is zero-based



self.ui.comboBox.setCurrentText("Second item") #select by text

QTableWidget

from PyQt5 import QtWidgets



from mydesign import Ui_MainWindow



import sys



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super(mywindow, self).__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.tableWidget.setColumnCount(2)



self.ui.tableWidget.setRowCount(4)





app = QtWidgets.QApplication([])



application = mywindow()



application.show()



sys.exit(app.exec()) #Clear QtableWidget content

def clear():



self.ui.tableWidget.clear()



self.ui.pushButton.clicked.connect(clear)

Populate QTableWidget by code

from PyQt5.QtWidgets import QTableWidgetItem



from mydesign import *



import sys



data = []



data.append(('Populating', 'QtableWidget'))



data.append(('With data', 'In Python'))



data.append(('Is easy', 'Job'))





class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super().__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.tableWidget.setRowCount(3)



self.ui.tableWidget.setColumnCount(2)



row=0



for tup in data:



col=0



for item in tup:



cellinfo=QTableWidgetItem(item)



self.ui.tableWidget.setItem(row, col, cellinfo)



col+=1



row += 1





app = QtWidgets.QApplication([])



win = mywindow()



win.show()



sys.exit(app.exec())

讓QTableWidget唯讀

from PyQt5.QtWidgets import QTableWidgetItem



from mydesign import *



import sys



data = []



data.append(('Populating', 'QtableWidget'))



data.append(('With data', 'In Python'))



data.append(('Is easy', 'Job'))





class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super().__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.tableWidget.setRowCount(3)



self.ui.tableWidget.setColumnCount(2)



row=0



for tup in data:



col=0



for item in tup:



cellinfo=QTableWidgetItem(item)



cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled) # make cell not editable



self.ui.tableWidget.setItem(row, col, cellinfo)



col+=1



row += 1





app = QtWidgets.QApplication([])



win = mywindow()



win.show()



sys.exit(app.exec())

設定QTableWidget的column名稱

from PyQt5.QtWidgets import QTableWidgetItem



from mydesign import *



import sys



data = []



data.append(('Populating', 'QtableWidget'))



data.append(('With data', 'In Python'))



data.append(('Is easy', 'Job'))





class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super().__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.tableWidget.setRowCount(3)



self.ui.tableWidget.setColumnCount(2)



self.ui.tableWidget.setHorizontalHeaderLabels(('Column 1', 'Column 2')) # set header text



row=0



for tup in data:



col=0



for item in tup:



cellinfo=QTableWidgetItem(item)



self.ui.tableWidget.setItem(row, col, cellinfo)



col+=1



row += 1





app = QtWidgets.QApplication([])



win = mywindow()



win.show()



sys.exit(app.exec())

在QTableWidget新增QComboBox

from PyQt5.QtWidgets import QTableWidgetItem



from mydesign import *



import sys



data = ['PyQt5','Is','Awesome']



class mywindow(QtWidgets.QMainWindow):



def __init__(self):



super().__init__()



self.ui = Ui_MainWindow()



self.ui.setupUi(self)



self.ui.tableWidget.setRowCount(3)



self.ui.tableWidget.setColumnCount(2)



row=0



for item in data:



cellinfo=QTableWidgetItem(item)



combo = QtWidgets.QComboBox()



combo.addItem("First item")



combo.addItem("Second item")



self.ui.tableWidget.setItem(row, 0, cellinfo)



self.ui.tableWidget.setCellWidget(row, 1, combo)



row += 1





app = QtWidgets.QApplication([])



win = mywindow()



win.show()



sys.exit(app.exec())

打包程式成.exe