• python GUI输入窗口


      为了解决 sublime text 下 python 的 raw_input() 函数无法起效,便萌生了个用 GUI 窗口来获取输入的想法,一开始想用 Tkinter,后来想了下还是用 PyQt 吧,一来代码量差不到哪里去,二来 Qt 显然更美观一些。封装成一个模块 Input.py:

    #!/usr/bin/env python
    #-*- coding: utf-8 -*-
    
    
    def getInput(label_str=None):
        '''Return the utf-8 string of text that you write in the lineEdit.
            label_str: the string as the prompt of the label in the dialog.'''
        from PyQt4 import QtGui, QtCore
        import sys
    
        if label_str == None:
            label_str = u'窗口以Unicode编码返回你所输入的信息:'
        else:
            label_str = unicode(label_str)
    
        class MyWindow(QtGui.QDialog):
    
            input_str = ''
    
            def __init__(self):
                QtGui.QDialog.__init__(self)
                self.setWindowTitle(u'GUI Input')
    
                self.label = QtGui.QLabel(label_str)
                self.lineEdit = QtGui.QLineEdit()
    
                self.ok = QtGui.QPushButton(u'确定')
                self.connect(self.ok, QtCore.SIGNAL('clicked()'), self.getLine)
    
                self.clean = QtGui.QPushButton(u'清空')
                self.connect(self.clean, QtCore.SIGNAL('clicked()'), self.cleaning)
    
                self.cancel = QtGui.QPushButton(u'取消')
                self.connect(self.cancel, QtCore.SIGNAL('clicked()'), self.quit)
    
                layout = QtGui.QGridLayout()
                layout.addWidget(self.label, 0, 0, 1, 4)
                layout.addWidget(self.lineEdit, 1, 0, 1, 4)
                layout.addWidget(self.ok, 2, 1, 1, 1)
                layout.addWidget(self.clean, 2, 2, 1, 1)
                layout.addWidget(self.cancel, 2, 3, 1, 1)
                self.setLayout(layout)
    
                MyWindow.input_str = ''
    
            def getLine(self):
                MyWindow.input_str = str(self.lineEdit.text().toUtf8())
                self.close()
    
            def cleaning(self):
                self.lineEdit.setText('')
    
            def quit(self):
                MyWindow.input_str = ''
                self.close()
    
        app = QtGui.QApplication(sys.argv)
        win = MyWindow()
        win.show()
        app.exec_()
        return MyWindow.input_str
    
    if __name__ == '__main__':
        pre_str = getInput()
        now_str = pre_str.decode('utf-8')
        print type(pre_str), type(now_str)
        print pre_str
        # print long(pre_str)
        # fp = open(now_str + '.txt', 'wb+')
        # fp.close()

      使用时只需要 import Input,然后使用 Input.getInput('xxx') 就行了,试了下还是能支持中文的,只需要安装 PyQt4 或者 PyQt5 模块就行了。效果如下:

      

      在输入框输入任何字符串后按确定就可以返回 Unicode 编码的 string,在 sublime text 下用 python 开发调试时就再也不用担心如何方便地进行输入的问题了。

  • 相关阅读:
    无向连通图求割边+缩点+LCA
    poj 1417(并查集+简单dp)
    java系统时间的调用和格式转换
    Delphi程序的主题(Theme)设置
    分块读取Blob字段数据(Oracle)
    Delphi xe5 编译报environment.proj错误的解决
    DataSnap服务器从xe2升级到xe5报错的处理
    分块读取Blob字段数据(MSSQL)
    Delphi XE5 for Android (十一)
    Delphi XE5 for Android (十)
  • 原文地址:https://www.cnblogs.com/Newdawn/p/5203113.html
Copyright © 2020-2023  润新知