Python: python的简单表达式计算器
这是一个简单的表达式计算器, 不太懂用Tkinter写GUI, 参考了别人的代码
from __future__ import division
from Tkinter import Tk, Entry, Button, Label, mainloop
from tkFont import Font
def get_value ():
v = ''
try:
v = eval(text.get()) #use eval to calculate the vlaue of the text
except:
pass
if isinstance(v, (int, float, long)):
pass
else:
v = 'Error...'
label.config(text = v) #use config to change the text
top = Tk()
top.title("calculator")
ft = Font(family = 'Courier New', size = 12)
text = Entry(top, font = ft)
button = Button(top, text = 'Ok', command = get_value)
label = Label(text = '(+ - * / % **)', font = ft)
Enter = lambda x: x.keycode == 13 and get_value()
Key = lambda x: label.config(text = '(+ - * / % **)')
text.bind('<Key>', Enter) #when the key is enter, execute the function Enter
text.focus()
text.bind('<Button-1>', Key) #when click the left-key, execute the function Key
text.pack()
button.pack()
label.pack()
mainloop()
相关文档:
赖勇浩(http://laiyonghao.com)
今天(2009年5月31日) OurPNP.org 搞了个聚会活动,弄了十几二十个人在广州海珠广场的堂会呆了五个小时,创下了我在 K 房呆的最长时间纪录。应他们的邀请,我做了个题为《用 python 快速搭建网游服务器》的小演讲,因为那边的电视竟然不能接电脑,所以讲的时候没有能够参照 PPT 来讲,观 ......
网络时间服务器 一般都遵循 RFC868 协议标准.
按该标准 附下面 Python 源码.
# -*- coding: utf-8 -*-
import socket,sys,time
#时间服务器
host = "stdtime.gov.hk"
#端口
port = 37
#时区
curtz = 8
#连接服务器,并接收返回
try:
host = socket.gethostbyname(host)
s = socket.socket(socket.AF_INET,soc ......
Python代码
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
......
闲来无事, 玩玩python...
是采用有道翻译, 然后抓取网页的.
import re, urllib
url="http://dict.youdao.com/search?le=eng&q="
print ("input q to exit")
while 1:
word = raw_input(">>>")
if word=="q":
exit()
else:
word = word.replace(' ', '+')
url += word
u ......
原帖:http://www.cnblogs.com/jingleguo/archive/2008/06/02/1211820.html
当python中间处理非ASCII编码时,经常会出现如下错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0x?? in position 1: ordinal not in range(128)
0x??是超出128的数字,python在默认的情况下认为语言的编码是ascii编码,所以无法 ......