建立 PyQt6 窗口

建立 PyQt6 窗口
這篇教學會介紹如何開始使用 PyQt6 建立基本的應用程序窗口,以及通過常用的窗口參數(shù),進行窗口的相關設定。
快速導航 :
建立 PyQt6 窗口
調(diào)整窗口樣式
在窗口中放入其他組件
建立 PyQt6 窗口
PyQt6 創(chuàng)建窗口的起手式,會先使用 QtWidgets.QApplication(sys.argv)
創(chuàng)建應用程序,接著使用 QtWidgets.QWidget()
放入基底組件,作為擺放其他組件的基底,窗口程序使用 sys.exit(app.exec())
作為結(jié)尾,意義上等同將程序包裹在一個無限循環(huán)里,下方的代碼執(zhí)行后,會出現(xiàn)一個空的窗口界面。
from?PyQt6?import?QtWidgets
import?sys
app?=?QtWidgets.QApplication(sys.argv)??
Form?=?QtWidgets.QWidget()?????????????
Form.show()????????????????????????????
sys.exit(app.exec())
改用 class 寫法:
from?PyQt6?import?QtWidgets
import?sys
class?MyWidget(QtWidgets.QWidget):
????def?__init__(self):
????????super().__init__()
if?__name__?==?'__main__':
????app?=?QtWidgets.QApplication(sys.argv)
????Form?=?MyWidget()
????Form.show()
????sys.exit(app.exec())
調(diào)整窗口樣式
創(chuàng)建窗口組件后,就能使用下列幾種常用方法設定樣式:
方法參數(shù)說明setWindowTitle()str設定標題。resize()width, height設定開啟窗口時的長寬。setStyleSheet()style使用網(wǎng)頁 CSS 樣式設置樣式 ( 不支持 CSS3 語法 )。width()取得窗口寬度。height()取得窗口高度。
下面的代碼執(zhí)行后,會產(chǎn)生一個 320x240 粉紅色背景的窗口,同時會印出該窗口的長寬。
from?PyQt6?import?QtWidgets
import?sys
app?=?QtWidgets.QApplication(sys.argv)
Form?=?QtWidgets.QWidget()
Form.setWindowTitle('千牛編程思維')??????#?設置標題
Form.resize(320,?240)???????????????????#?設置大小
Form.setStyleSheet('background:#fcc;')??#?設置樣式
print(Form.width())?????????????????????
print(Form.height())???????????????????
Form.show()
sys.exit(app.exec())
改用 class 寫法:
from?PyQt6?import?QtWidgets
import?sys
class?MyWidget(QtWidgets.QWidget):
????def?__init__(self):
????????super().__init__()
????????self.setWindowTitle('千牛編程思維')??????
????????self.resize(320,?240)???????????????????
????????self.setStyleSheet('background:#fcc;')??
????????print(self.width())?????????????????????
????????print(self.height())???????????????????
if?__name__?==?'__main__':
????app?=?QtWidgets.QApplication(sys.argv)
????Form?=?MyWidget()
????Form.show()
????sys.exit(app.exec())

在窗口中放入其他組件
建立基底的組件后,就能在里面加入其他的組件,下方的代碼執(zhí)行后,會在窗口里加入一個文字為 hello world 的 Label。
from?PyQt6?import?QtWidgets
import?sys
app?=?QtWidgets.QApplication(sys.argv)
Form?=?QtWidgets.QWidget()
Form.setWindowTitle('千牛編程思維')
Form.resize(320,?240)
Form.setStyleSheet('background:#fcc;')
label?=?QtWidgets.QLabel(Form)??????????#?在?Form?里添加label
label.move(50,50)???????????????????????#?移動到?(50,?50)?的位置
label.setText('hello?world')????????????#?寫入文字
label.setStyleSheet('font-size:30px;?color:#00c')??#?設定樣式
Form.show()
sys.exit(app.exec())
改用 class 寫法:
from?PyQt6?import?QtWidgets
import?sys
class?MyWidget(QtWidgets.QWidget):
????def?__init__(self):
????????super().__init__()
????????self.setWindowTitle('千牛編程思維')
????????self.resize(320,?240)
????????self.setStyleSheet('background:#fcc;')
????????self.ui()
????def?ui(self):
????????self.label?=?QtWidgets.QLabel(self)??????????
????????self.label.move(50,50)???????????????????????
????????self.label.setText('hello?world')????????????
????????self.label.setStyleSheet('font-size:30px;?color:#00c')??
if?__name__?==?'__main__':
????app?=?QtWidgets.QApplication(sys.argv)
????Form?=?MyWidget()
????Form.show()
????sys.exit(app.exec())
