zoukankan      html  css  js  c++  java
  • Pyqt4学习笔记-对话框(更新ing)

    QInputDialog:可交互输入单个值的对话框。输入值可以是一个字符串,一个数字或者列表的一项。

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class InputExample(QtGui.QWidget):
    
        def __init__(self):
            super(InputExample, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.button = QtGui.QPushButton('Dialog', self)
            self.button.setFocusPolicy(QtCore.Qt.NoFocus)
            # 设置聚焦策略,NoFocus表示不接受焦点
    
            self.button.move(20, 20)
            self.connect(self.button, QtCore.SIGNAL('clicked()'),
                         self.showDialog)
            self.setFocus()
            # 信号接收之后(调用方法中的组件)获取焦点?
    
            self.label = QtGui.QLineEdit(self)
            self.label.setFocusPolicy(QtCore.Qt.NoFocus)
            # 设置聚焦策略,NoFocus表示不接受焦点
            self.label.move(130, 22)
    
            self.setWindowTitle('InputDialog')
            self.setGeometry(300, 300, 350, 80)
    
    
        def showDialog(self):
            # 初始化输入框,定义输入框Title,提示字符
            text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
                                                  'Enter your name:')
    
            # ok 在上面作为if的标识用,和显示出来的效果无关
            # 输入框显示的默认都是ok,cancel
            if ok:
                self.label.setText(str(text))
                # 如果点击确认,输入的值会出现在上面的label中
    
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        ex = InputExample()
        ex.show()
        app.exec_()

    QFileDialog:允许用户选择文件或文件夹,可选择文件来打开和保存

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class OpenFileExample(QtGui.QMainWindow):
    
        def __init__(self):
            super(OpenFileExample, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.textEdit = QtGui.QTextEdit()
            self.setCentralWidget(self.textEdit)
            self.statusBar()
            self.setFocus()
    
            openFile = QtGui.QAction(QtGui.QIcon('icons/open.png'), 'Open', self)
            openFile.setShortcut('Ctrl+O')
            openFile.setStatusTip('Open new File')
            self.connect(openFile, QtCore.SIGNAL('triggered()'), self.showDialog)
    
            menubar = self.menuBar()
            fileMenu = menubar.addMenu('&File')
            fileMenu.addAction(openFile)
    
            self.setGeometry(300, 300, 350, 300)
            self.setWindowTitle('OpenFile')
    
        def showDialog(self):
    
            filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
                                                         '/home')
            # 第一个字符串是标题,第二个字符串指定对话框的工作目录,在windows下用/home也是可以的
            fname = open(filename)
            # 不能直接打开路径中包含中文的文件,会报错
            data = fname.read()
            # 读取文件
            self.textEdit.setText(data)
            # 显示在文本域中
    
    app = QtGui.QApplication(sys.argv)
    ex = OpenFileExample()
    ex.show()
    app.exec_()

    之后再更新。

  • 相关阅读:
    Eureka实现注册与发现
    SpringBoot之彩色输出
    SpringBoot之简单日志配置
    SpringBoot之文件读取
    springboot之banner
    记一下一个我误解的小特性
    spring4新特性-泛型依赖注入
    boot之mybatis
    springmvc之单元测试(MockMvc)-独立测试
    git ignore
  • 原文地址:https://www.cnblogs.com/shadow-ccos/p/5208643.html
Copyright © 2011-2022 走看看