Qt5中窗口類型分爲三類 QMainWindow、QWidget、QDialog。
- QMainWindow:繼承自QWidget,窗口包含菜單欄、工具欄、狀態欄、標題欄等,是GUI主窗口。不能設置佈局。
addToolBar() |
添加工具欄 |
centralWidget() |
返回窗口中心的一個控件,未設置時返回NULL |
menuBar() |
返回主窗口菜單欄 |
setCentralWidget() |
設置窗口中心控件 |
statusBar() |
獲得狀態欄對象,調用狀態欄對象showMessage(msg,timeout)在狀態欄顯示消息。 參數一爲顯示的消息內容,參數二爲顯示幾秒,單位爲毫秒,默認0,表示一直顯示該消息 |
案例1:在主窗口創建一個狀態欄
# -*- coding: utf-8 -*-
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtGui import QIcon
class MainWindow(QMainWindow):
def __init__(self,parent=None):
super(MainWindow,self).__init__(parent)
#窗口大小
self.resize(400,200)
# 創建狀態欄
self.status=self.statusBar()
#將信息顯示在狀態欄,顯示時間爲5秒,之後消失
self.status.showMessage("狀態欄",5000)
self.setWindowTitle("pyqt QMainWindow test")
if __name__=="__main__":
app=QApplication(sys.argv)
app.setWindowIcon(QIcon("./images/cartoon1.ico"))
form=MainWindow()
form.show()
sys.exit(app.exec_())
案例二:將主窗口放在屏幕中間顯示,並點擊按鈕關閉窗口
#將主窗口放在屏幕中間顯示,並點擊按鈕關閉窗口
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QDesktopWidget,QApplication,QMainWindow,QPushButton,QHBoxLayout,QWidget
import sys
class WinForm(QMainWindow):
def __init__(self,parent=None):
super(WinForm,self).__init__(parent)
self.setWindowTitle("主窗口放在屏幕中間顯示")
self.resize(370,250)
self.center()
#添加一個button
self.button1=QPushButton("按鈕")
#添加一個按鈕的點擊的信號與槽事件,在槽函數中獲取QApplication類對象,
#調用quit關閉窗口
self.button1.clicked.connect(self.onButtonClick)
layout=QHBoxLayout()
layout.addWidget(self.button1)
main_frame=QWidget()
main_frame.setLayout(layout)
self.setCentralWidget(main_frame)
def center(self):
#計算屏幕大小
screen=QDesktopWidget().screenGeometry()
#獲得窗口大小
size=self.geometry()
self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)
def onButtonClick(self):
#獲取發送對象--按鈕控件
sender=self.sender()
print(sender.text()+"被按下")
qApp=QApplication.instance()
qApp.quit()
if __name__=="__main__":
app=QApplication(sys.argv)
win=WinForm()
win.show()
sys.exit(app.exec_())
- QWidget: 適用於窗口類型暫不確定,或作爲頂層窗口,或嵌入其他窗口中。是所有用戶界面對象的基類。
- QDialog:對話框基類,用來執行短期任務,或者與用戶互動,顯示方式分爲模態、非模態。無菜單欄、工具欄、狀態欄。
x() y() |
獲得窗口左上角的座標 |
width() height() |
獲得客戶區的寬度和高度 |
QWdiget提供的geometry()對象的函數x() y() |
獲取客戶去左上角的座標, |
QWdiget提供的geometry()對象的函數width() height() |
獲得客戶區的寬度與高度 |
QWdiget提供的frameGeometry()對象的函數x() y() |
獲得窗口左上角座標 |
QWdiget提供的frameGeometry()對象的函數width() height() |
獲得包含客戶區、標題欄和邊框在內的整個窗口的寬度和高度 |
QWidget在邊界客戶區需要注意的地方:
- QWidget不包含邊框的常用函數
不包含邊框的部分是客戶區,用戶可以操作的地方,添加子控件。其位置是指在屏幕上的位置,在Qt中保存和使用的時QRect類。該類有自己的大小和位置
- 改變客戶去面積:
QWidget.resize(width,height)
QWidget.resize(QSize)
- 獲得客戶區大小:QWidget.size()
- 獲得客戶去的寬度和高度:
QWidget.width()
QWidget.height()
獲得客戶去的寬度和高度
QWidget.width()
QWidget.height()
設置客戶去的寬度和高度
QWidget.setFixedWidth(int width) 高度不變
QWidget.setFixedHeight(int height) 寬度不變
寬高都固定,不可以通過鼠標改變窗口寬高
QWidget.setFixedSize(QSize size)
QWidget.setFixedSize(int width,int height)
改變客戶區大小
Qwidget.setGeometry(int x,int y,int width,int height)
QWidget.setGeometry(QRect rect)
包含邊框的常用函數
- 獲得窗口大小和位置 QWidget.frameGeometry()
- 設置窗口位置
QWidget.move(int x,int y)
QWidget.move(QPoint point)
獲得窗口左上角座標 QWidget.pos()
案例一:屏幕座標系統顯示
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QApplication,QPushButton,QWidget
import sys
app=QApplication(sys.argv)
widget=QWidget()
btn=QPushButton(widget)
btn.setText("Button")
#以QWidget左上角爲(0,0)點
btn.move(20,20)
#不通的操作系統對窗口的最小寬度有規定,若設置寬度小於規定值,則會以規定值進行顯示
widget.resize(300,200)
#以屏幕左上角爲遠點
widget.move(250,200)
widget.setWindowTitle("pyqt座標系統")
widget.show()
print("w.x()=%d" % widget.x())
print("w.y()=%d" % widget.y())
print("w.width()=%d" % widget.width())
print("w.height()=%d" % widget.height())
print("QWidget.geometry")
print("w.geometry().x()=%d" % widget.geometry().x())
print("w.geometry().y()=%d" % widget.geometry().y())
print("w.geometry().width()=%d" % widget.geometry().width())
print("w.geometry().height()=%d" % widget.geometry().height())
sys.exit(app.exec_())