栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python QT5 3- 基本操作 - 2.按钮 Button

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python QT5 3- 基本操作 - 2.按钮 Button

2. 按钮 Button

>> Buttons包括 6 类

  • Push Button(按钮)
  • Tool Button(工具按钮)-- 图标显示,可以定义popupmenu和toolbuttonstyle(实战未使用)
  • Radio Button(单选框)-- 单选框
  • Check Box(复选框)-- 复选框
  • Command Link Button(命令链接)--  导航按钮(实战未使用)
  • Dialog Button Box(对话框按钮)-- 系统快速按钮部署(实战未使用)

 >> 基本属性

属性说明
QobjectobjectName按钮对像引用名
Qwidgetenabled是否可操作(置灰)
geometry显示形状
sizePolicy尺寸水平 垂直伸展策略
minimumSize最小尺寸
maximumSize最大尺寸
sizeIncementUI变化时尺寸变化
baseSize基本尺寸,默认0
palette姿色设置
font设置字体
cursor鼠标形状
mouseTracking响应鼠标移动事件
tabletTracking平板?
focusPolicy对像获取焦点选项
contextMenuPolicy上下文菜单?
acceptDrops平板?
toolTiptip提示设置
toolTipDurationtip显示时间
statusTip状态栏tip显示
whatsThishelp文本显示?
accesibleName特殊人群使用
accesibleDescription特殊人群使用
layoutDirection布局方向
autoFillBackground对像是否填充背景区域
styleSheet样式设置
locale语言,国家 地区
inputMethodHints输入法设置
QAbstractButtontext设置文本显示,字符前加 & 表示快捷键 alt +
icon设置图标
iconSize图标大小
shortcut设置多字符快捷键
checkable是否可以被选中,true时每次点击转换check 和 uncheck状态
checked是否被选中
autoRepeat是否重复点击,与autorepetdelay,autorepeatinterval组合使用
QPushButtonautoDefault 
default 
flat是否显示边框

>> 声明

self.pbopenfile = QtWidgets.QPushButton(self.frame) #define push button

self.pbopenfile.setMinimumSize(QtCore.QSize(0, 30)) #size
self.pbopenfile.setObjectName("pbopenfile")         #button name 
self.verticalLayout.addWidget(self.pbopenfile)      #layout
self.pbopenfile.setToolTip("Open File...")          #tooltip   

self.pbopenfile.clicked.connect(self.openfile)      #click event

##################
self.pbopenfile.setText(_translate("MainWindow", "Open File")) #show text

>> 事件响应

self.pbopenfile.clicked.connect(self.openfile)      #click event

##set content container
self.twshowfile = QTreeWidget(self.frame_2)         #tree view
self.teshowfile = QtWidgets.QTextEdit(self.frame_2) #test edit

##################################   
def openfile(self):

## 响应按钮并弹出提示框 QMessageBox
    # msg = QMessageBox.information(None, 'Info', 'Information',QMessageBox.Ok|QMessageBox.Cancel|QMessageBox.Yes|QMessageBox.No|QMessageBox.Abort|QMessageBox.Retry|QMessageBox.Ignore,QMessageBox.No)
    #
    # if msg == QMessageBox.Yes:
    #     print("Yes")
    # elif msg == QMessageBox.No:
    #     print("No")

## 打开文件 OpenFileDialog
    # QFileDialog.getOpenFileName(None, 'Open file', './')
    fileName, filetype = QFileDialog.getOpenFileName(None,"Select File","./","Text Files (*.txt;*.jpg)")  # 设置文件扩展名过滤,双分号间隔

    ## 获取打开文件名
    self.leopenfile.setText(fileName)


    # path = QtCore.QStringListModel()
    # pathlist = []

    ## 设置文件显示容器
    self.twshowfile.setColumnCount(1)
    root = QTreeWidgetItem(self.twshowfile)

    ## 打开文件并显示在容器中
    with open(fileName,"r",encoding="utf-8") as fp:
        fprl = fp.readlines()
        fprl.reverse()

        for f in fprl:
            tf = f
            f = f.replace('r',"").replace('t',"").replace('n',"")
            self.teshowfile.append(f)
            # pathlist.append(f)
            if fprl.index(tf) == 0:
                self.teshowfile.append(">>>>>>>>>>>>>>>>>>>>>>>>>>")
                self.twshowfile.setHeaderLabels([f])
                root.setText(0,">>>>>>>>>>>>>>>>>>>>>>>>>>")
            else:
                qitem = QTreeWidgetItem()
                qitem.setText(0,f)
                root.addChild(qitem)

    self.twshowfile.expandAll()

    self.teshowfile.moveCursor(QTextCursor.Start)
    self.teshowfile.setFocus()

参考:第15.14节 PyQt(Python+Qt)入门学习:Designer的Buttons按钮详解_mb5fd86853067b7的技术博客_51CTO博客

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/968705.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号