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 开发调试时就再也不用担心如何方便地进行输入的问题了。
python GUI输入窗口的更多相关文章
- Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) JAVA日志的前世今生 .NET MVC采用SignalR更新在线用户数 C#多线程编程系列(五)- 使用任务并行库 C#多线程编程系列(三)- 线程同步 C#多线程编程系列(二)- 线程基础 C#多线程编程系列(一)- 简介
Python GUI之tkinter窗口视窗教程大集合(看这篇就够了) 一.前言 由于本篇文章较长,所以下面给出内容目录方便跳转阅读,当然也可以用博客页面最右侧的文章目录导航栏进行跳转查阅. 一.前言 ...
- 转载:python原生态的输入窗口抖动+输入特效
python原生态的输入窗口抖动+输入特效 出处:https://coding.net/u/acee/p/PythonPowerInput/git/blob/master/test_power_inp ...
- python GUI初步
- 使用PyQt来编写第一个Python GUI程序
原文:使用PyQt来编写第一个Python GUI程序 本文由 伯乐在线 - Lane 翻译,Daetalus 校稿.未经许可,禁止转载!英文出处:pythonforengineers.com.欢迎加 ...
- python GUI图形化编程-----wxpython
一.python gui(图形化)模块介绍: Tkinter :是python最简单的图形化模块,总共只有14种组建 Pyqt :是python最复杂也是使用最广泛的图形化 Wx ...
- Python GUI 编程
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
- Python GUI编程(Tkinter) windows界面开发
Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...
- python GUI尝鲜(但当涉猎,见往事耳)
第一步:简单的窗口和内容 import tkinter as tk window = tk.Tk() # 窗口obj对象 window.title('my TK') # 窗口名字 window.geo ...
- Python GUI编程(Tkinter)(一)
tk官网的教程学习: https://tkdocs.com/tutorial/firstexample.html 学习blog: https://www.cnblogs.com/aland-1415/ ...
随机推荐
- com.caucho.hessian.io.HessianProtocolException: is unknown code 解决方案
问题: Cannot access Hessian remote service at [http://....../remote/syllabusService]; nested exception ...
- Mybatis传多个参数(三种解决方案)
http://blog.csdn.net/liangyihuai/article/details/49965869 (zhuan)
- apache2.4 windows764 python cgi
修改conf下的httpd.conf; 1:Listen和ServerName修改为相同的端口号,如8066 2:ScriptAlias就让他留在原位置,"${SRVROOT}/cgi-bi ...
- 《zw版·Halcon-delphi系列原创教程》 zw版-Halcon常用函数Top100中文速查手册
<zw版·Halcon-delphi系列原创教程> zw版-Halcon常用函数Top100中文速查手册 Halcon函数库非常庞大,v11版有1900多个算子(函数). 这个Top版,对 ...
- vs2010问题:未能安装xxx包
打开vs2010新建c++工程,出现问题如图 原因是重复安装了,之前安装的没有删除干净,导致冲突. 如果你的vs2010安装在c盘,解决方法:http://blog.sina.com.cn/s/blo ...
- PHP 生成验证码
//加载 vendor目录的phpqrcode.类文件 $a = vendor("phpqrcode.qrlib");// 创建目录 echo mkdir(__ROOT__.'/U ...
- 自定义置顶TOP按钮
简述一下,分为三个步骤: 1. 添加Html代码 2. 调整Css样式 3. 添加Jquery代码 具体代码如下: <style type="text/css"> #G ...
- python 学习
python 使用 缩进 代替 C 中的 {} 或 delphi 中的 begin...end 1.help() 显示帮助或 help(<命令>) 2.字符串前加 r 表示原始字符串, ...
- C#:复杂条件判断类型(练习)
/// <summary> /// 文件类型 /// </summary> public enum FileType { Courseware, //"课件" ...
- C# Winform中DataGridView绑定后DataGridViewCheckBoxColumn无法显示的问题
在控件DataGridView绑定数据源后,发现DataGridViewCheckBoxColumn不能显示当前的check值.经过一番努力,现将完整代码奉献出来,仅供参考. 错误代码: /*禁止自动 ...