首页 > 编程语言 > 详细

python PyQt5

时间:2020-02-08 21:33:11      阅读:96      评论:0      收藏:0      [点我收藏+]
# _*_ coding:utf-8 _*_
import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout,
                             QPushButton, QMainWindow)


class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.setWindowTitle(‘关闭主窗口‘)
        # 设置窗口标题
        self.resize(600, 600)

        layout = QVBoxLayout()
        self.close_btn = QPushButton(‘关闭‘, self)

        layout.addWidget(self.close_btn)

        main_frame = QWidget()
        self.setCentralWidget(main_frame)
        main_frame.setLayout(layout)

        self.close_btn.clicked.connect(self.on_button_click)
        self.status = self.statusBar()
        self.status.showMessage(‘我是状态栏‘, 5000)

    def on_button_click(self):
        # 将信息显示在状态栏中
        sender = self.sender()

        msg = sender.text() + " 被按下了"
        status = self.statusBar()
        status.showMessage(msg, 5000)
        #self.showMaximized()
        

        # 程序退出
        app = QApplication.instance()
        app.quit()


if __name__ == "__main__":
    app = QApplication(sys.argv[1:])

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

  ===============================

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QColor, QFont, QIcon, QKeySequence
from PyQt5.QtCore import Qt, QDate, QTime, QDateTime, QTimer
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog, QPrintPreviewDialog
import os


class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUI()

    def initUI(self):
        self.setWindowTitle("主窗口示例")
        # self.resize(400,250)

        self.setup_centralWidget()
        self.setup_dockWidget()
        self.setup_statusBar()

        self.createActions()
        self.setup_menuBar()
        self.setup_toolBar()

    def setup_centralWidget(self):
        # 设置主窗口中心部件
        self.textEdit = QTextEdit()  # 初始化时换行符无效
        self.textEdit.setText("第一行\n第二行\n第三行\n")
        # self.textEdit.selectAll()
        self.setCentralWidget(self.textEdit)  # 指定主窗口中心部件

    def createActions(self):  # 创建动作
        # 打开文件动作
        self.fileOpenAction = QAction(QIcon("open.png"), "&Open", self)  # 动作在self中可用
        self.fileOpenAction.setShortcut(QKeySequence.Open)  # 设置标准化快捷键
        # self.fileOpenAction.setShortcut("Ctrl+O")# windows 系统下等效于
        self.fileOpenAction.setToolTip("打开文件")  # 工具栏提示文本
        self.fileOpenAction.setStatusTip("打开现有的文件")  # 状态栏提示文本
        self.fileOpenAction.triggered.connect(self.fileOpen)

        # 文件另存为动作
        self.fileSaveAsAction = QAction(QIcon("save.png"), "SaveAs", self)  # 动作在self中可用
        self.fileSaveAsAction.setShortcut(QKeySequence.SaveAs)  # 设置标准化快捷键
        # windows 系统下无快捷键
        self.fileSaveAsAction.setToolTip("另存为")  # 工具栏提示文本
        self.fileSaveAsAction.setStatusTip("文件另存为")  # 状态栏提示文本
        self.fileSaveAsAction.triggered.connect(self.fileSaveAs)

        # 动作分隔符
        self.separator = QAction(self)
        self.separator.setSeparator(True)

    def setup_menuBar(self):
        # 文件菜单
        self.fileMenu = self.menuBar().addMenu("&File")
        self.fileMenu.addAction(self.fileOpenAction)
        self.fileMenu.addAction(self.fileSaveAsAction)
        # self.fileMenu.addAction(self.separator)

    def fileOpen(self):
        fileName, filetype = QFileDialog.getOpenFileName(self,
                                                         "打开文件",
                                                         os.getcwd(),  # 设定起始路径为当前目录
                                                         "Text Files (*.txt)")  # 设置文件扩展名过滤,用双分号间隔
        if fileName != "":
            with open(fileName) as f:
                self.textEdit.setText(f.read())

    def fileSaveAs(self):
        fileName, filetype = QFileDialog.getSaveFileName(self,
                                                         "文件另存为",
                                                         os.getcwd(),  # 起始路径
                                                         "Text Files (*.txt);;Html Files (*.html)")  # 设置文件扩展名过滤,用双分号间隔
        if fileName == "": return
        if filetype == "Text Files (*.txt)":
            with open(fileName, "w") as f:
                f.write(self.textEdit.toPlainText())
        elif filetype == "Html Files (*.html)":
            with open(fileName, "w") as f:
                f.write(self.textEdit.toHtml())

    def setup_toolBar(self):
        fileToolbar = self.addToolBar("File")  # 添加工具条;可依次添加多个工具条
        fileToolbar.addActions((self.fileOpenAction, self.fileSaveAsAction, self.separator))  # 添加动作
        tb_label1 = QLabel("选择什么东东")
        self.cb = QComboBox()
        self.cb.addItems(("", "绿", ""))
        fileToolbar.addWidget(tb_label1)  # 工具条添加部件
        fileToolbar.addWidget(self.cb)  # 工具条添加部件
        # ......

    def setup_dockWidget(self):
        # 设置停靠窗口
        self.logDockWidget = QDockWidget("log", self)
        self.logDockWidget.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        self.listWidget = QListWidget()
        self.logDockWidget.setWidget(self.listWidget)
        self.addDockWidget(Qt.RightDockWidgetArea, self.logDockWidget)  # 添加停靠窗口

    def setup_statusBar(self):
        # 配置状态栏
        self.dateLabel = QLabel()
        self.dateLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
        self.dateLabel.setText(QDate.currentDate().toString())

        self.timeLabel = QLabel()
        self.timeLabel.setFrameStyle(QFrame.StyledPanel | QFrame.Sunken)
        self.timeLabel.setText(QTime.currentTime().toString())

        self.statusBar().addPermanentWidget(self.dateLabel)
        self.statusBar().addPermanentWidget(self.timeLabel)

        datetime = QDateTime.currentDateTime().toString()
        self.statusBar().showMessage(datetime, 3000)  # 状态栏在3000ms内显示信息
        self.statusBar().showMessage("ready")  # 状态栏显示信息

        timer = QTimer(self)  # 创建计时器#self
        timer.timeout.connect(self.updateBySecond)  # 计时器超时信号接到用于更新界面的槽
        timer.start(1000)  # 每1000ms发射超时信号

    def updateBySecond(self):
        self.timeLabel.setText(QTime.currentTime().toString())
        self.dateLabel.setText(QDate.currentDate().toString())


if __name__ == __main__:
    app = QApplication(sys.argv)
    mw = MainWindow()
    mw.show()
    sys.exit(app.exec_())

#本文分享自微信公众号 - Python

 

python PyQt5

原文:https://www.cnblogs.com/gisoracle/p/12285139.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!