• python gui


    基于tkinter完成一个简易计算器
    1 至少包含+-*/运算
    2 能正确计算机结果

    运行截图如下:

    代码如下:
    import tkinter as tk

    class jsj:
    def init(self):
    self.root = tk.Tk()
    self.root.title('简易计算器')
    self.mylist = []
    self.result = tk.StringVar()
    self.result.set(0)
    self.layout()

        self.root.mainloop()
    
    def put(self, x):
        self.mylist.append(x)
        self.result.set(''.join(self.mylist))
    
    def delete(self):
        self.mylist.clear()
        self.result.set(0)
    
    def back(self):
        if len(self.mylist) > 0:
            del self.mylist[-1]
            self.result.set(self.mylist)
    
    def calculation(self):
        expression = ''.join(self.mylist)
        result = eval(expression)
        self.result.set(result)
        self.mylist.clear()
        self.mylist.append(str(result))
    
    def operate(self, operator):
        if len(self.mylist) > 0:
            if self.mylist[-1] in ['+', '-', '*', '/', '.']:
                self.mylist[-1] = operator
            else:
                self.mylist.append(operator)
            self.result.set(''.join(self.mylist))
    
    def layout(self):
        label = tk.Label(self.root, textvariable=self.result, width=20, height=2, justify='left', anchor='se')
        label.grid(row=0, column=0, padx=4, pady=4, columnspan=4)
        button_clear = tk.Button(self.root, text='C', width=12, command=self.delete)
        button_clear.grid(row=1, column=0, columnspan=2, padx=4, pady=4)
        button_back = tk.Button(self.root, text='←', width=5, command=self.back)
        button_back.grid(row=1, column=2, padx=4, pady=4)
        button_div = tk.Button(self.root, text='/', width=5, command=lambda: self.operate('/'))
        button_div.grid(row=4, column=3, padx=4, pady=4)
        button_mult = tk.Button(self.root, text='*', width=5, command=lambda: self.operate('*'))
        button_mult.grid(row=3, column=3, padx=4, pady=4)
        button_seven = tk.Button(self.root, text=7, width=5, command=lambda: self.put('7'))
        button_seven.grid(row=2, column=0, padx=4)
        button_eight = tk.Button(self.root, text=8, width=5, command=lambda: self.put('8'))
        button_eight.grid(row=2, column=1, padx=4)
        button_nine = tk.Button(self.root, text=9, width=5, command=lambda: self.put('9'))
        button_nine.grid(row=2, column=2, padx=4)
        button_sub = tk.Button(self.root, text='-', width=5, command=lambda: self.operate('-'))
        button_sub.grid(row=2, column=3, padx=4, pady=4)
        button_four = tk.Button(self.root, text=4, width=5, command=lambda: self.put('4'))
        button_four.grid(row=3, column=0, padx=4)
        button_five = tk.Button(self.root, text=5, width=5, command=lambda: self.put('5'))
        button_five.grid(row=3, column=1, padx=4)
        button_six = tk.Button(self.root, text=6, width=5, command=lambda: self.put('6'))
        button_six.grid(row=3, column=2, padx=4)
        button_add = tk.Button(self.root, text='+', width=5, command=lambda: self.operate('+'))
        button_add.grid(row=1, column=3, padx=4, pady=4)
        button_one = tk.Button(self.root, text=1, width=5, command=lambda: self.put('1'))
        button_one.grid(row=4, column=0, padx=4)
        button_two = tk.Button(self.root, text=2, width=5, command=lambda: self.put('2'))
        button_two.grid(row=4, column=1, padx=4)
        button_three = tk.Button(self.root, text=3, width=5, command=lambda: self.put('3'))
        button_three.grid(row=4, column=2, padx=4)
        button_equal = tk.Button(self.root, text='=', width=12, command=self.calculation)
        button_equal.grid(row=5, column=2, columnspan=2, padx=4, rowspan=5)
        button_zero = tk.Button(self.root, text=0, width=5, command=lambda: self.put('0'))
        button_zero.grid(row=5, column=1, padx=4)
        button_pot = tk.Button(self.root, text='.', width=5, command=lambda: self.put('.'))
        button_pot.grid(row=5, column=0, padx=4, pady=4)
    

    jsj()

  • 相关阅读:
    using可以用于释放操作,相当于Dispose()
    Entity Framework 5.0 Code First全面学习
    .NET Entity Framework入门操作
    李克强:让科技人员合理合法富起来
    THC=TERMINAL HANDLING CHARGE,碼頭操作費
    到处寻找移动互联网的风口:站在台风口,猪都能飞上天,为什么互联网能在家装行业产生这么巨大的冲击力?
    群雄逐鹿 互联网家装市场烽烟四起
    要给出互联网解决社会性问题的步骤与方法
    Spring Cloud 2-Hystrix DashBoard仪表盘(五)
    Spring Cloud 2-Hystrix 断路容错保护(四)
  • 原文地址:https://www.cnblogs.com/weihehahaha/p/14129080.html
Copyright © 2020-2023  润新知