Tkinter图形界面设计


#coding=gbk

from Tkinter import *
import pycurl
import sys, re, md5, os, time, commands
import pycurl
import cStringIO as _StringIO
import sys
import shutil, urllib, urllib2
import socket,sqlite3 as sqlite
import datetime, time
from time import sleep
site='12530'
# MIME type
accept_type = "*/*"
# http transfer limits
connection_timeout = 500
timeout = 1000
low_speed = 200
low_speed_time = 120
max_size = 20485760 # 10MB
root=Tk()
cwd=StringVar(root)
mobile=StringVar(root)
#three=StringVar(root)
text=Entry(root,width=50,textvariable=cwd)
l1=Label(root,text=u"请输入手机号码")
#textmobile=Entry(root,width=50,textvariable=mobile)
textmobile=Text(root,width=50,height=15)
l2=Label(root,text=u"请输入三位数字")
three=Text(root,width=50,height=5)
#text.pack()
l1.pack()
textmobile.pack()
l2.pack()
three.pack()
bfm=Frame(root)
t=Text(root,height=10,background='black',foreground='green')

def dols():
m=textmobile.get('0.0',END)
m=m.strip()
ss=m.split('\n')
ths=three.get('0.0',END)
ths=ths.strip('\n')
sth=ths.split('\n')
count=0
for th in sth:
for s in ss:
if len(s.strip()) == 0 : continue
t.insert('0.0', u"使用号码%s和%s抓取\n" % (s.strip(),th))

print url
v,c,type=get(url,count)
t.update()
sleep(1)
count=count+1
t.insert('0.0',u"抓取已完成,请换批号码再提交\n")

ok=Button(bfm,text=u'提交',activeforeground='white',
activebackground='green',command=dols)
ok.pack()
bfm.pack()

t.pack()
def get_curl(user_agent="MSIE"):
"initialize curl handle"
curl_handle = pycurl.Curl()
curl_handle.setopt(pycurl.FOLLOWLOCATION, 1)
curl_handle.setopt(pycurl.MAXREDIRS, 5)
curl_handle.setopt(pycurl.CONNECTTIMEOUT, connection_timeout)
curl_handle.setopt(pycurl.TIMEOUT, timeout)
curl_handle.setopt(pycurl.NOSIGNAL, 1)
curl_handle.setopt(pycurl.LOW_SPEED_LIMIT, 100)
curl_handle.setopt(pycurl.LOW_SPEED_TIME, low_speed_time)
curl_handle.setopt(pycurl.HTTPHEADER, ["User-Agent: %s" % user_agent, accept_type])
curl_handle.setopt(pycurl.MAXFILESIZE, max_size)
#curl_handle.setopt(pycurl.PROXY, "10.0.0.172")
#curl_handle.setopt(pycurl.PROXYPORT,80)
#curl_handle.setopt(PROXYUSERPWD,"dummyuser:dummypasswd")

return curl_handle


def curl_fetch(curl_handle, url,count):
"retrieve url, return the content, http code, time, effective url"
fp = _StringIO.StringIO()
curl_handle.setopt(pycurl.URL, url)
curl_handle.setopt(pycurl.WRITEFUNCTION, fp.write)

# perform the transfer
try:
curl_handle.perform()
except pycurl.error, e:
print e
return (-1, "", 0)

content_type = curl_handle.getinfo(pycurl.CONTENT_TYPE)
#print curl_handle.getinfo(pycurl.HTTP_CODE)
#t.insert('0.0', u"第 %d 次:Download speed: %.2f bytes/second\n" % (count,curl_handle.getinfo(curl_handle.SPEED_DOWNLOAD)) )
t.insert('0.0',content_type)
t.insert('0.0',u"大小是%s字节\n" %(len(fp.getvalue())) )
t.update()
print content_type
return (0,fp.getvalue(), content_type)

def get(url,count):
handle=get_curl()
v,c,t=curl_fetch(handle,url,count)
handle.close()
return(v,c,t)

root.mainloop()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这是一个VB6的IDE插件(Addin),使用VB6的IDE直接设计Python的界面。 Python和VB都是能让人快乐的编程语言,我使用了Python之后,很多自己使用的工具都使用Python开发或改写了,因为最终实现的Python代码实在太短了(相比VB),有时候Python一行代码就可以实现VB一个函数的功能。 Python就是这种让人越用越开心的语言。 不过说实在,使用Python开发GUI界面还是麻烦了一些了,自带的标准库Tkinter使用起来非常简单,不过对于习惯了VB拖放控件完成界面设计的偶来说,还是不够人性化。TK也有一个工具叫GUI Builder,不过它使用Layout布局,不够直观,而且用起来很不爽。。 至于PyQt/wxPython等GUI库,尽管有可视化设计工具,但总感觉做一般的轻量级应用是杀鸡用牛刀, 而且不够环保,不够低碳,要带一个很大的库,需要目标机器上夜同样安装了PyQt/wxPython,做不了绿色软件。 所以最终的结果是我更喜欢Tkinter,用起来很简单,绿色环保,真正的跨平台,一个py文件到处运行(担心泄密就编译成pyc)。 很多人都认为TK的界面不够美观,不过我经过多次实验后发现导入Python自带的标准TTK主题库,界面非常Native,不输PyQt/wxPython。 此Addin默认启用TTK支持,也可选择关闭。 总而言之,轻量级GUI,TK+TTK足够。 使用此Addin,你可以不用写一句代码就可以生成一个完整可运行的Python的GUI界面,支持2.X和3.X。 安装方法:将压缩包解压到你希望的目录,然后执行Setup.exe完成注册插件过程,打开VB6就可以用了。 在VB窗体上设计完成界面后(你可以大胆的设置各控件的属性,Addin尽量将其翻译为tkinter的控件属性),点工具栏上的VisualTkinter(图标为一片羽毛),再点'生成代码'按钮,即可生成可运行的Python代码,可以拷贝至剪贴板或保存至文件。 一般情况下你可以不用再改变tkinter的控件属性,但是如果你熟悉tkinter,需要更多的控制,可以一一核对各属性,并且修改,再生成代码。 当然除了用来设计界面外,此ADDIN内置的各控件属性列表可以做为编程参考,比较完整,除了极少数我认为大多数人都不用的属性外,属性定义基本上是我从官方的tkinter文档直接翻译的。 如果还没有VB6,网上找一个VB6精简版即可,不到20M,小巧玲珑。 代码已经在Github上托管,更新的版本可以在这上面找到,需求也可以在上面提: https://github.com/cdhigh/Visual-Tkinter-for-Python
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值