学习了一天的tk,现在简单的做一个小玩意,来,小二,上代码:
# -*- coding:utf-8 -*-
import tkinter
# 创建窗口
tk = tkinter.Tk()
# 给窗口起个名字
tk.title('计算器')
# 尺寸
tk.geometry('300x250')
# 标题
biaoqian = tkinter.Label(tk, text='第一个程序').pack()
var1 = tkinter.StringVar()
# var1.set('请输入第一个数')
d1 = tkinter.Entry(tk, textvariable=var1, show=None).pack()
var2 = tkinter.StringVar()
# var2.set('请输入第二个数')
d2 = tkinter.Entry(tk, textvariable=var2, show=None).pack()
var = tkinter.StringVar()
var.set('等于:')
shuchu = tkinter.Label(tk, textvariable=var, bg='yellow', width=22, height=2).place(x=70, y=110)
def jia():
# 加
g1 = var1.get() # get方法是获取内容
g2 = var2.get()
g1 = int(g1)
g2 = int(g2)
return var.set(g1+g2) # set方法是输出内容
def jian():
# 减
g1 = var1.get()
g2 = var2.get()
g1 = int(g1)
g2 = int(g2)
return var.set(g1-g2)
def cheng():
# 乘
g1 = var1.get()
g2 = var2.get()
g1 = int(g1)
g2 = int(g2)
return var.set(g1*g2)
def chu():
# 除
g1 = var1.get()
g2 = var2.get()
g1 = int(g1)
g2 = int(g2)
return var.set(g1/g2)
an1 = tkinter.Button(tk, text=' + ', command=jia).place(x=80, y=70)
an2 = tkinter.Button(tk, text=' - ', command=jian).place(x=120, y=70)
an3 = tkinter.Button(tk, text=' X ', command=cheng).place(x=160, y=70)
an4 = tkinter.Button(tk, text=' / ', command=chu).place(x=200, y=70)
tk.mainloop()
运行一下:
虽然不美观,但是够用就好,哈哈