Python GUI编程(TKinter)(简易计算器)

搞课设搞得心累,现在看到人脸这两个字就烦躁,无聊搞搞tkinter,实现一个计算器的功能,能够简单的加减乘除。

简单的页面如下:

 Python GUI编程(TKinter)(简易计算器)_第1张图片

简单的代码如下:

 1 # encoding:utf-8
 2 import tkinter as tk
 3 
 4 root = tk.Tk()
 5 root.title("Small Calculator")
 6 root.geometry('500x300')
 7 
 8 var1 = tk.DoubleVar()
 9 label1 = tk.Label(text="The First Number", width=20, height=2)
10 label1.pack()
11 entry1 = tk.Entry(width=20)
12 entry1.pack()
13 
14 var2 = tk.StringVar()
15 label2 = tk.Label(text="The Operator", width=20, height=2)
16 label2.pack()
17 entry2 = tk.Entry(width=20)
18 entry2.pack()
19 
20 var3 = tk.DoubleVar()
21 label3 = tk.Label(text="The Second Number", width=20, height=2)
22 label3.pack()
23 entry3 = tk.Entry(width=20)
24 entry3.pack()
25 
26 var4 = tk.DoubleVar()
27 label4 = tk.Label(text="The Solution", width=20, height=2)
28 label4.pack()
29 label5 = tk.Label(textvar=var4, width=30, height=1,)
30 label5.pack()
31 
32 def clickFun():
33     global var1
34     global var2
35     global var3
36     global var4
37     va11 = int(entry1.get())
38     var2 = entry2.get()
39     var3 = int(entry3.get())
40     if var2 == '+':
41         var4.set(int(va11+var3))
42     elif var2 == '-':
43         var4.set(int(va11-var3))
44     elif var2 == '*':
45         var4.set(int(va11*var3))
46     elif var2 == '/':
47         if var3 == 0:
48             var4.set("请重新输入数据")
49         else:
50             var4.set(int(va11/var3))
51     else:
52         var4.set("运算符出错,请重新输入数据")
53 
54 button1 = tk.Button(text="Deal" ,width=10, height=2, command=clickFun)
55 button1.pack()
56 root.mainloop()

 

转载于:https://www.cnblogs.com/shixinzei/p/11096157.html

你可能感兴趣的:(Python GUI编程(TKinter)(简易计算器))