主窗口类型
主窗口类型 有3种窗口 QMainWindow QWidget QDialog QMainWindow:可以包含菜单栏、工具栏、状态栏和标题栏,是最常见的窗口形式 QDialog:是对话窗口的基类。没有菜单栏、工具栏、状态栏。 QWidget:不确定窗口的用途,就使用QWidget。
第一个主窗口
import sys from PyQt5.QtWidgets import QMainWindow,QApplication from PyQt5.QtGui import QIcon class FirstMainWin(QMainWindow): def __init__(self): super(FirstMainWin,self).__init__() # 设置主窗口的标题 self.setWindowTitle('第一个主窗口应用') # 设置窗口的尺寸 self.resize(400,300) self.status = self.statusBar() self.status.showMessage('只存在5秒的消息',5000) if __name__ == '__main__': app = QApplication(sys.argv) app.setWindowIcon(QIcon('./images/Dragon.ico')) main = FirstMainWin() main.show() sys.exit(app.exec_())
窗口居中显示
# QDesktopWidget import sys from PyQt5.QtWidgets import QDesktopWidget,QMainWindow,QApplication from PyQt5.QtGui import QIcon class CenterForm(QMainWindow): def __init__(self): super(CenterForm,self).__init__() # 设置主窗口的标题 self.setWindowTitle('让窗口居中') # 设置窗口的尺寸 self.resize(400,300) def center(self): # 获取屏幕坐标系 screen = QDesktopWidget().screenGeometry() # 获取窗口坐标系 size = self.geometry() newLeft = (screen.width() - size.width()) / 2 newTop = (screen.height() - size.height()) / 2 self.move(newLeft,newTop) if __name__ == '__main__': app = QApplication(sys.argv) main = CenterForm() main.show() sys.exit(app.exec_())
退出应用程序
import sys from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QPushButton,QWidget class QuitApplication(QMainWindow): def __init__(self): super(QuitApplication,self).__init__() self.resize(300,120) self.setWindowTitle('退出应用程序') # 添加Button self.button1 = QPushButton('退出应用程序') # 将信号与槽关联 self.button1.clicked.connect(self.onClick_Button) layout = QHBoxLayout() layout.addWidget(self.button1) mainFrame = QWidget() mainFrame.setLayout(layout) self.setCentralWidget(mainFrame) # 按钮单击事件的方法(自定义的槽) def onClick_Button(self): sender = self.sender() print(sender.text() + ' 按钮被按下') app = QApplication.instance() # 退出应用程序 app.quit() if __name__ == '__main__': app = QApplication(sys.argv) main = QuitApplication() main.show() sys.exit(app.exec_())
屏幕坐标系
import sys from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QPushButton,QWidget def onClick_Button(): print("1") print("widget.x() = %d" % widget.x()) # 250 (窗口横坐标) print("widget.y() = %d" % widget.y()) # 200 (窗口纵坐标) print("widget.width() = %d" % widget.width()) # 300(工作区宽度) print("widget.height() = %d" % widget.height()) # 240 (工作区高度) print("2") print("widget.geometry().x() = %d" % widget.geometry().x()) # 250 (工作区横坐标) print("widget.geometry().y() = %d" % widget.geometry().y()) # 222 (工作区纵坐标) print("widget.geometry().width() = %d" % widget.geometry().width() ) # 300(工作区宽度) print("widget.geometry().height() = %d" % widget.geometry().height()) # 240 (工作区高度) print("3") print("widget.frameGeometry().x() = %d" % widget.frameGeometry().x()) # 250 (窗口横坐标) print("widget.frameGeometry().y() = %d" % widget.frameGeometry().y()) # 200 (窗口纵坐标) print("widget.frameGeometry().width() = %d" % widget.frameGeometry().width() ) # 300(窗口宽度) print("widget.frameGeometry().height() = %d" % widget.frameGeometry().height()) # 262(窗口高度) app = QApplication(sys.argv) widget = QWidget() btn = QPushButton(widget) btn.setText("按钮") btn.clicked.connect(onClick_Button) btn.move(24,52) widget.resize(300,240) # 设置工作区的尺寸 widget.move(250,200) widget.setWindowTitle('屏幕坐标系') widget.show() sys.exit(app.exec_())
设置窗口图标
import sys from PyQt5.QtWidgets import QMainWindow,QApplication from PyQt5.QtGui import QIcon ''' 窗口的setWindowIcon方法用于设置窗口的图标,只在Windows中可用 QAplication中的setWindowIcon方法用于设置主窗口的图标和应用程序图标,但调用了窗口的setWindowIcon方法 QAplication中的setWindowIcon方法就只能用于设置应用程序图标了 ''' class IconForm(QMainWindow): def __init__(self): super(IconForm,self).__init__() self.initUI() def initUI(self): self.setGeometry(300,300,250,250) # 设置主窗口的标题 self.setWindowTitle('设置窗口图标') # 设置窗口图标 self.setWindowIcon(QIcon('./images/Basilisk.ico')) if __name__ == '__main__': app = QApplication(sys.argv) app.setWindowIcon(QIcon('./images/Dragon.ico')) main = IconForm() main.show() sys.exit(app.exec_())控件添加提示信息
# 显示控件提示消息 import sys from PyQt5.QtWidgets import QHBoxLayout,QMainWindow,QApplication,QToolTip,QPushButton,QWidget from PyQt5.QtGui import QFont class TooltipForm(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): QToolTip.setFont(QFont('SansSerif',12)) self.setToolTip('今天是<b>星期五</b>') self.setGeometry(300,30,200,300) self.setWindowTitle('设置控件提示消息') self.button1 = QPushButton('我的按钮') self.button1.setToolTip('这是一个按钮,Are you ok?') layout = QHBoxLayout() layout.addWidget(self.button1) mainFrame = QWidget() mainFrame.setLayout(layout) self.setCentralWidget(mainFrame) if __name__ == '__main__': app = QApplication(sys.argv) main = TooltipForm() main.show() sys.exit(app.exec_())