计算机术语tk,Tkinter[简单计算机,能初步了解Tkinter]

# -*- coding: cp936

-*-

#python 2.7

#转载:http://ju.outofmemory.cn/tag/python?page=88

from Tkinter import *

# 按键返回函数

def call(num):

content = display.get() + num

display.set(content)

# 使用eval 函数计算

def calculate():

try:

content =

display.get()

result =

eval_r(content)

display.set(content + '=\n' + str(result))

except:

display.set('Error')

clear()

# 清空内容栏

def clear():

display.set('')

# 删除前一个字符

def backspace():

display.set(str(display.get()[:-1]))

def main():

# 定义主窗口

root = Tk()

root.title('Calculator')

root.geometry('210x200+300+400')

# 将display定义成global,main() 函数外的call,

calculate等可以调用

global display

display = StringVar()

# 设置内容显示栏,使用label,anchor是靠右,默认居中

label = Label(root, relief = 'sunken',

borderwidth = 3, anchor = SE)

label.config(bg = 'grey', width = 25, height =

3)

label['textvariable'] = display

label.grid(row = 0, column = 0, columnspan =

4)

#   text = Text(root, relief =

'sunken', borderwidth = 3)

#   text.insert(INSERT,

str(display))

#   text.grid(row = 0, column =

0, columnspan = 4)

# 添加各个按钮,并绑定行为,使用lambda很方便,是用的是grid布局

Button(root, text = 'C', fg = '#EF7321', width =

3, command = lambda: clear()).grid(row = 1, column = 0)

Button(root, text = 'DEL', width = 3, command =

lambda:backspace()).grid(row = 1, column = 1)

Button(root, text = '/', width = 3, command =

lambda:call('/')).grid(row = 1, column = 2)

Button(root, text = '*', width = 3, command =

lambda:call('*')).grid(row = 1, column = 3)

Button(root, text = '7', width = 3, command =

lambda:call('7')).grid(row = 2, column = 0)

Button(root, text = '8', width = 3, command =

lambda:call('8')).grid(row = 2, column = 1)

Button(root, text = '9', width = 3, command =

lambda:call('9')).grid(row = 2, column = 2)

Button(root, text = '-', width = 3, command =

lambda:call('-')).grid(row = 2, column = 3)

Button(root, text = '4', width = 3, command =

lambda:call('4')).grid(row = 3, column = 0)

Button(root, text = '5', width = 3, command =

lambda:call('5')).grid(row = 3, column = 1)

Button(root, text = '6', width = 3, command =

lambda:call('6')).grid(row = 3, column = 2)

Button(root, text = '+', width = 3, command =

lambda:call('+')).grid(row = 3, column = 3)

Button(root, text = '1', width = 3, command =

lambda:call('1')).grid(row = 4, column = 0)

Button(root, text = '2', width = 3, command =

lambda:call('2')).grid(row = 4, column = 1)

Button(root, text = '3', width = 3, command =

lambda:call('3')).grid(row = 4, column = 2)

Button(root, text = '=', width = 3, bg =

'#EF7321', height = 3,command = lambda:calculate()).grid(row = 4,

column = 3, rowspan = 2)

Button(root, text = '0', width = 10, command =

lambda:call('0')).grid(row = 5, column = 0, columnspan =

2)

Button(root, text = '.', width = 3, command =

lambda:call('.')).grid(row = 5, column =

2)

root.mainloop()

if __name__ ==

'__main__':

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值