pyqt5学习笔记(四)

本文介绍了PyQt5中的几种对话框使用,包括QInputDialog的getDouble、getInt、getItem和getMultiLineText方法,QFileDialog的文件选择对话框,以及QFontDialog和QColorDialog的简单应用。
摘要由CSDN通过智能技术生成

对话框

标准输入对话框 QInputDialog

几个常用的方法:

  • getDouble(QWidget, str, str, value: float = 0, min: float = -2147483647, max: float = 2147483647, decimals: int = 1, flags: Union[Qt.WindowFlags,Qt.WindowType]=Qt.WindowFlags())->Tuple[float,bool]

parent – 指定父组件

str – 对话框标题名

str – 对话框的标签提示

min – 标准float类型的最小值

max – 标准float类型的最大值

decimals – 小数点后面保留的位数, 默认为1位

falgs – 指定对话框的样式

==返回元组(float, bool)==


  • getInt(QWidget, str, str, value: int = 0, min: int = -2147483647, max: int = 2147483647, step: int = 1, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags()) -> Tuple[int, bool]

parent – 指定父组件。

str – 对话框标题名。

str – 对话框的标签提示。

min – int类型最小值。

max – int类型最大值。

step – 数据变化的步长。

falgs – 指定对话框的样式。

==返回元组(int, bool)==


  • getItem(QWidget, str, str, Iterable[str], current:int=0,editable:bool=True,flags:Union[Qt.WindowFlags,Qt.WindowType]=Qt.WindowFlags(),inputMethodHints:Union[Qt.InputMethodHints,Qt.InputMethodHint] = Qt.ImhNone) -> Tuple[str, bool]

parent – 指定父组件。

str – 对话框标题名。

str – 对话框的标签提示。

Iterable[str] – 选择对话框中的可选择list。

editable – 标准条目选择对话框是否可编辑标志, 默认为不可编辑。

flags – 指明标准输入对话框的窗体标识。

inputMethodHints – 通过选择不同的inputMethodHints值来实现不同的键盘布局。


  • getMultiLineText(QWidget, str, str, text: str = ”, flags: Union[Qt.WindowFlags, Qt.WindowType] = Qt.WindowFlags(), inputMethodHints: Union[Qt.InputMethodHints, Qt.InputMethodHint] = Qt.ImhNone)-> Tuple[str, bool]

parent – 指定父组件。

str – 对话框标题名。

str – 对话框的标签提示。

text – 输入对话框中的默认值。

flags – 指明标准输入对话框的窗体标识。

inputMethodHints – 通过选择不同的inputMethodHints值来实现不同的键盘布局。



# -*- coding: utf-8 -*-
# @Date    : 2018/6/4 19:28 
# @Author  : yw 


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.label = QLabel("姓名:", self)
        self.label.move(20, 100)

        self.edit = QLineEdit(self)
        self.edit.move(80, 100)

        self.bt = QPushButton("提交", self)
        self.bt.move(60, 220)

        self.bt.clicked.connect(self.showDialog)
        self.setGeometry(200, 200, 300, 300)
        self.setWindowTitle("inputDialog")
        self.show()

    def showDialog(self):

        text, ok = QInputDialog.getText(self, "QinputDialog", "enter your name:")

        if ok:
            self.edit.setText(str(text))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


文件对话框 QFileDialog


# -*- coding: utf-8 -*-
# @Date    : 2018/6/4 19:28 
# @Author  : yw 


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Example(QWidget):

    def __init__(self):
        super().__init__()
        self.color = QColor(0, 0, 0)
        self.bt = QPushButton("open file", self)
        self.bt1 = QPushButton("open files", self)
        self.bt2 = QPushButton("open directory", self)
        self.bt3 = QPushButton("save file", self)

        self.filedit = QLineEdit("hello python")
        self.filesedit = QLineEdit("hello files")
        self.dir_edit = QLineEdit("hello directory")

        layout = QGridLayout()
        layout.addWidget(self.bt, 0, 0)
        layout.addWidget(self.filedit, 0, 1)
        layout.addWidget(self.bt1, 1, 0)
        layout.addWidget(self.filesedit, 1, 1)

        layout.addWidget(self.bt2, 2, 0)
        layout.addWidget(self.dir_edit, 2, 1)
        layout.addWidget(self.bt3, 3, 0)

        self.bt.clicked.connect(self.OpenFile)
        self.bt1.clicked.connect(self.OpenFiles)
        self.bt2.clicked.connect(self.OpenDir)
        self.bt3.clicked.connect(self.savefile)

        self.setLayout(layout)
        self.setGeometry(200, 200, 500, 500)
        self.setWindowTitle("demo")
        self.show()

    def OpenFile(self):
        filename = QFileDialog.getOpenFileName(self, "打开文件", "./", "All Files (*);;Text Files (*.)")

        self.filedit.setText(str(filename[0]))

    def OpenFiles(self):
        filename = QFileDialog.getOpenFileNames(self, "打开多个文件", "./", "All Files (*);;Text Files (*.)")

        self.filesedit.setText(str(filename[0]))

    def OpenDir(self):
        dirname = QFileDialog.getExistingDirectory(self, "打开目录", "c:/")

        self.dir_edit.setText(dirname)

    def savefile(self):
        file = QFileDialog.getSaveFileName(self, "保存文件", "./")


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

代码分解下:

getOpenFileName(parent: QWidget = None, caption: str = ”, directory: str = ”, filter: str = ”, initialFilter: str = ”, options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[str, str]

  • parent, 指定父组件,一般用self。
  • caption, 对话框的标题。
  • directory, 默认打开的目录。
  • filter, 文件后缀名过滤器。
  • initialFilter, 默认文件过滤器。
  • options, 对话框的的一些参数设定。比如只显示文件夹。

filename = QFileDialog.getOpenFileName(self, “打开文件”, “./”)

==返回一个元组(文件名, 文件过滤器)。==

还有一些常用的方法,参数都是类似。
getOpenFileNames(parent: QWidget = None, caption: str = ”, directory: str = ”, filter: str = ”, initialFilter: str = ”, options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[List[str], str]

filename = QFileDialog.getOpenFileNames(self, “打开多个文件”, “./”, “All Files ();;Text Files (.)”)

==返回值仍然是一个元组([文件名1, … ,文件名n], 文件过滤器)==

getExistingDirectory(parent: QWidget = None, caption: str = ”, directory: str = ”, options: Union[QFileDialog.Options, QFileDialog.Option] = QFileDialog.ShowDirsOnly) -> str

dirname = QFileDialog.getExistingDirectory(self, “打开目录”, “c:/”)

==返回目录名==

getSaveFileName(parent: QWidget = None, caption: str = ”, directory: str = ”, filter: str = ”, initialFilter: str = ”, options: Union[QFileDialog.Options, QFileDialog.Option] = 0) -> Tuple[str, str]

字体对话框 QFontDialog

颜色对话框 QColorDialog


import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.button = QPushButton("我是按钮")
        self.bt1 = QPushButton("字体")
        self.bt2 = QPushButton("颜色")

        layout = QVBoxLayout()
        layout.addWidget(self.button)
        layout.addWidget(self.bt1)
        layout.addWidget(self.bt2)
        self.setLayout(layout)

        self.bt1.clicked.connect(self.choiceFont)
        self.bt2.clicked.connect(self.choiceColor)
        self.setGeometry(200, 200, 500, 500)
        self.setWindowTitle("demo")
        self.show()


    def choiceFont(self):
        font, ok = QFontDialog().getFont()
        if ok:
            self.button.setFont(font)

    def choiceColor(self):
        self.color = QColorDialog.getColor()
        if self.color.isValid():
            self.button.setPalette(QPalette(self.color))
            self.button.setAutoFillBackground(True)



if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值