[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import Menu
from tkinter import ttk
class GUI:
def __init__(self):
self.root = tk.Tk()
self.root.title('计算器v0.1')
self.root.geometry("250x400+1100+150")
self.root.resizable(False, False) # 禁止最大化
# 创建主菜单实例
self.menubar = Menu(self.root)
# 显示菜单,将root根窗口的主菜单设置为menu
self.root.config(menu=self.menubar)
style = ttk.Style(self.root)
style.configure("TButton", font=("Helvetica", 20))
self.interface()
def interface(self):
""""界面编写位置"""
self.lab = tk.Label(self.root, text="",font=("黑体", 26, "bold"))
self.lab.place(x=5, y=200)
#self.asss = eval(self.lab.cget("text"))
#print(self.asss)
self.w1 = tk.Text(self.root,width=33, height=15)
self.w1.place(x=7, y=0)
self.w1.insert("insert", "")
self.w1.tag_configure("font", font=("黑体", 20))
self.w1.tag_add("font", "1.0", "end")
self.btn1 = ttk.Button(self.root, text="1", command=self.anniu1)
self.btn1.place(x=10, y=270,relwidth=0.2, relheight=0.1)
# 按钮2
self.btn2 = ttk.Button(self.root, text="2", command=self.anniu2)
self.btn2.place(x=65, y=270,relwidth=0.2, relheight=0.1)
self.btn2 = ttk.Button(self.root, text="5", command=self.anniu5)
self.btn2.place(x=65, y=317, relwidth=0.2, relheight=0.1)
self.btn2 = ttk.Button(self.root, text="8", command=self.anniu8)
self.btn2.place(x=65, y=364, relwidth=0.2, relheight=0.1)
# 按钮3
self.btn3 = ttk.Button(self.root, text="3", command=self.anniu3)
self.btn3.place(x=120, y=270,relwidth=0.2, relheight=0.1)
self.btn3 = ttk.Button(self.root, text="6", command=self.anniu6)
self.btn3.place(x=120, y=317, relwidth=0.2, relheight=0.1)
self.btn3 = ttk.Button(self.root, text="9", command=self.anniu9)
self.btn3.place(x=120, y=364, relwidth=0.2, relheight=0.1)
self.btn4 = ttk.Button(self.root, text="4", command=self.anniu4)
self.btn4.place(x=10, y=317, relwidth=0.2, relheight=0.1)
self.btn7 = ttk.Button(self.root, text="7", command=self.anniu7)
self.btn7.place(x=10, y=364, relwidth=0.2, relheight=0.1)
self.btn3 = ttk.Button(self.root, text="AC", command=self.anniuac)
self.btn3.place(x=10, y=235, relwidth=0.2, relheight=0.08)
self.btn3 = ttk.Button(self.root, text="x", command=self.anniux)
self.btn3.place(x=65, y=235, relwidth=0.2, relheight=0.08)
self.btn3 = ttk.Button(self.root, text="÷", command=self.anniuc)
self.btn3.place(x=120, y=235, relwidth=0.2, relheight=0.08)
self.btn3 = ttk.Button(self.root, text="-", command=self.anniujian)
self.btn3.place(x=175, y=235, relwidth=0.27, relheight=0.08)
self.btn3 = ttk.Button(self.root, text="+", command=self.anniujia)
self.btn3.place(x=175, y=270, relwidth=0.27, relheight=0.1)
self.btn3 = ttk.Button(self.root, text="=", command=self.anniud)
self.btn3.place(x=175, y=317, relwidth=0.27, relheight=0.1)
self.btn3 = ttk.Button(self.root, text="0", command=self.anniu0)
self.btn3.place(x=175, y=364, relwidth=0.27, relheight=0.1)
self.btn3 = ttk.Button(self.root, text="清空", command=self.anniudqk)
self.btn3.place(x=175, y=200, relwidth=0.27, relheight=0.08)
def anniudqk(self):
self.w1.delete(1.0, tk.END)
def anniud(self):
if self.lab.cget("text") == '':
pass
else:
if self.shu[-1] == '-' or self.shu[-1] == '+' or self.shu[-1] == '*' or self.shu[-1] == '/':
pass
else:
if '+' in self.shu or '-' in self.shu or '*' in self.shu or '/' in self.shu:
qq = eval(self.shu)
self.w1.insert("insert", "%s=%s\n"% (self.shu,qq))
self.w1.tag_configure("font", font=("黑体", 26))
self.w1.tag_add("font", "1.0", "end")
self.lab.config(text="")
else:
pass
def anniu1(self):
self.shu=self.lab.cget("text")
self.lab.config(text="1")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s"%self.shu)
def anniu2(self):
self.shu = self.lab.cget("text")
self.lab.config(text="2")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu3(self):
self.shu = self.lab.cget("text")
self.lab.config(text="3")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu4(self):
self.shu = self.lab.cget("text")
self.lab.config(text="4")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu5(self):
self.shu = self.lab.cget("text")
self.lab.config(text="5")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu6(self):
self.shu = self.lab.cget("text")
self.lab.config(text="6")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu7(self):
self.shu = self.lab.cget("text")
self.lab.config(text="7")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu8(self):
self.shu = self.lab.cget("text")
self.lab.config(text="8")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu9(self):
self.shu = self.lab.cget("text")
self.lab.config(text="9")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniu0(self):
self.shu = self.lab.cget("text")
self.lab.config(text="0")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniuac(self):
self.lab.config(text="")
def anniujia(self):
if self.lab.cget("text") == '':
pass
else:
if self.shu[-1] == '-' or self.shu[-1] == '+' or self.shu[-1] == '*' or self.shu[-1] == '/':
pass
else:
self.shu = self.lab.cget("text")
self.lab.config(text="+")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniujian(self):
if self.lab.cget("text") == '':
pass
else:
if self.shu[-1] == '-' or self.shu[-1] == '+' or self.shu[-1] == '*' or self.shu[-1] == '/':
pass
else:
self.shu = self.lab.cget("text")
self.lab.config(text="-")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniux(self):
if self.lab.cget("text") == '':
pass
else:
if self.shu[-1] == '-' or self.shu[-1] == '+' or self.shu[-1] == '*' or self.shu[-1] == '/':
pass
else:
self.shu = self.lab.cget("text")
self.lab.config(text="*")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
def anniuc(self):
if self.lab.cget("text") == '':
pass
else:
if self.shu[-1] == '-' or self.shu[-1] == '+' or self.shu[-1] == '*' or self.shu[-1] == '/':
pass
else:
self.shu = self.lab.cget("text")
self.lab.config(text="/")
self.shu = self.shu + self.lab.cget("text")
self.lab.config(text="%s" % self.shu)
if __name__ == '__main__':
a = GUI()
a.root.mainloop()