PyQt5
PyQt5
QtGui
- 设置程序图标 :QIcon
- 显示气泡提示信息:QFont
1、将qtdisigner生成的ui文件转换成py文件
1 pyuic5 -o firstMainWin.py firstMainWin.ui
2、创建主窗口
import sys from PyQt5.QtWidgets import QMainWindow,QApplication class MainWindow(QMainWindow): def __init__(self,parent=None): super(MainWindow,self).__init__() self.resize(400,200)#设置主窗口大小 self.status = self.statusBar()#获得状态栏对象 self.status.showMessage("这是状态栏提示",5000)#调用状态栏对象,5000为设置提示5秒,默认为0,表示永久显示 self.setWindowTitle("PyQt MainWindow 例子")#设置窗口标题 if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec())
3、主窗口居中显示
#coding:utf-8 import sys from PyQt5.QtWidgets import QApplication,QWidget,QMainWindow,QDesktopWidget class Winform(QMainWindow): def __init__(self): super(Winform,self).__init__() self.setWindowTitle("主窗口居中显示") self.resize(370,250) self.center()#调用center函数 def center(self): screen = QDesktopWidget().screenGeometry()#获取屏幕大小 size = self.geometry()#获取窗口大小 self.move((screen.width()-size.width())/2,(screen.height()-size.height())/2)#移动窗口到指定位置 if __name__ == '__main__': app = QApplication(sys.argv) win = Winform() win.show() sys.exit(app.exec())
3、关闭主窗口
import sys from PyQt5.QtWidgets import QMainWindow,QHBoxLayout,QPushButton,QApplication,QWidget class WinForm(QMainWindow): def __init__(self): super(WinForm,self).__init__(parent=None) self.setWindowTitle("关闭主窗口例子") self.button1 = QPushButton('关闭主窗口') self.button1.clicked.connect(self.onButtonClick) layout = QHBoxLayout() layout.addWidget(self.button1) main_frame = QWidget() main_frame.setLayout(layout) self.setCentralWidget(main_frame) def onButtonClick(self):
#sender是发送信号的对象,此处发送信号的对象是button1按钮 sender = self.sender() print(sender.text()+'被按下了') qApp = QApplication.instence() qApp.quit() if __name__ == '__main__': app = QApplication(sys.argv) form = WinForm() form.show() sys.exit(app.exec())
4、屏幕坐标显示
import sys from PyQt5.QtWidgets import QApplication,QWidget,QPushButton app = QApplication(sys.argv) widget = QWidget() widget.setWindowTitle("屏幕坐标系统") btn = QPushButton(widget) btn.setText('Button') widget.resize(300,200) btn.move(20,20) widget.move(250,200) widget.show() print("QWiget:") 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("QWidget.geometry,x()=%d"%widget.geometry().x()) print("QWidget.geometry,y()=%d"%widget.geometry().y()) print("QWidget.geometry,width()=%d"%widget.geometry().width()) print("QWidget.geometry,height()=%d"%widget.geometry().height()) sys.exit(app.exec())
5、设置程序图标
import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QWidget , QApplication #1 class Icon(QWidget): def __init__(self, parent = None): super(Icon,self).__init__(parent) self.initUI() #2 def initUI(self): self.setGeometry(300, 300, 250, 150) self.setWindowTitle('演示程序图标例子') self.setWindowIcon(QIcon('./images/cartoon1.ico')) if __name__ == '__main__': app = QApplication(sys.argv) icon = Icon() icon.show() sys.exit(app.exec_())
6、PyQt5气泡提示
#coding:utf-8 ''' 【简介】 PyQT5中气泡提示 ''' import sys from PyQt5.QtWidgets import QWidget, QToolTip , QApplication from PyQt5.QtGui import QFont class Winform(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): QToolTip.setFont(QFont('宋体', 10)) self.setToolTip('这是一个<b>气泡提示</b>') self.setGeometry(200, 300, 400, 400) self.setWindowTitle('气泡提示demo') if __name__ == '__main__': app = QApplication(sys.argv) win = Winform() win.show() sys.exit(app.exec_())
7、QLabel
方法 | 描述 |
setAlignment() |
按固定值方式对齐文本:
|
setIndent() | 设置文本缩进值 |
setPixmap() | 设置QLabel为一个Pixmap图片 |
text() | 获得QLabel的文本内容 |
setText() | 设置QLabel的文本内容 |
selectText() | 返回所选择的字符 |
setBuddy() | 设置QLabel的助记符及buddy(伙伴),即使用QLabel设置快捷键,会在快捷键后将焦点设置到其buddy上,这里用到了QLabel的交互控件功能。此外,buddy可以是任何一个Widget控件。使用setBuddy(QWidget *)设置,其QLabel必须是文本内容,并且使用“&”符号设置了助记符。 |
setWordWrap() | 设置是否允许换行 |