鱼C论坛

 找回密码
 立即注册
查看: 1116|回复: 2

[技术交流] 看了github的例子,自己写了个计算器,很简陋,只能进行简单的一次运算,可自己补充

[复制链接]
发表于 2023-4-21 10:59:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 kno 于 2023-4-21 21:56 编辑
import io  # used for dealing with input and output
from tkinter import *  # importing the necessary libraries
import tkinter.messagebox as mbox
import tkinter as tk  # imported tkinter as tk


# -----------------------------------------------------------------------------------------------



class Keypad(tk.Frame):


    keyboardnum=[i for i in range(0,10)]
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.target = None
        self.memory = ""

        for x, item in enumerate(self.keyboardnum):
            b = tk.Button(
                self,
                text=item,
                command=lambda text=item: self.append(text),
                font=("Arial", 14),
                bg="white",
                fg="black",
                borderwidth=3,
                relief="raised",
            )
            b.grid(row=(x%3), column=int(x/3), sticky="news")

        x = tk.Button(
            self,
            text="+",
            command=self.add_sym,
            font=("Arial", 14),
            bg="white",
            fg="black",
            borderwidth=3,
            relief="raised",
        )
        x.grid(row=0, column=10, columnspan="2", sticky="news")

        x = tk.Button(
            self,
            text="-",
            command=self.dec_sym,
            font=("Arial", 14),
            bg="white",
            fg="black",
            borderwidth=3,
            relief="raised",
        )
        x.grid(row=0, column=12, columnspan="2", sticky="news")

        x = tk.Button(
            self,
            text="*",
            command=self.mul_sym,
            font=("Arial", 14),
            bg="white",
            fg="black",
            borderwidth=3,
            relief="raised",
        )
        x.grid(row=0, column=14, columnspan="3", sticky="news")

        x = tk.Button(
            self,
            text="/",
            command=self.div_sym,
            font=("Arial", 14),
            bg="white",
            fg="black",
            borderwidth=3,
            relief="raised",
        )
        x.grid(row=0, column=17, columnspan="2", sticky="news")

        x = tk.Button(
            self,
            text="=",
            command=self.equ_sym,
            font=("Arial", 14),
            bg="white",
            fg="black",
            borderwidth=3,
            relief="raised",
        )
        x.grid(row=0, column=19, columnspan="2", sticky="news")




    def get(self):
        if self.target:
            return self.target.get()

    def append(self, text):
        if self.target:
            self.target.insert("end", text)

    def clear(self):
        if self.target:
            self.target.delete(0, END)

    def backspace(self):
        if self.target:
            text = self.get()
            text = text[:-1]
            self.clear()
            self.append(text)

    def space(self):
        if self.target:
            text = self.get()
            text = text + " "
            self.clear()
            self.append(text)

    def copy(self):
        # TODO: copy to clipboad
        if self.target:
            self.memory = self.get()
            self.label["text"] = "memory: " + self.memory
            print(self.memory)

    def paste(self):
        # TODO: copy from clipboad
        if self.target:
            self.append(self.memory)

    def show(self, entry):
        self.target = entry
        self.place(relx=0.4, rely=0.4, anchor="c")



    def add_sym(self):
        if self.target:
            text=self.get()
            text=text+"+"
            self.clear()
            self.append(text)

    def dec_sym(self):
        if self.target:
            text=self.get()
            text=text+"-"
            self.clear()
            self.append(text)

    def mul_sym(self):
        if self.target:
            text=self.get()
            text=text+"*"
            self.clear()
            self.append(text)

    def div_sym(self):
        if self.target:
            text=self.get()
            text=text+"/"
            self.clear()
            self.append(text)

    def equ_sym(self):
        if self.target:
            equ_result()
            self.clear()


window = tk.Tk()
window.title("Emoji Dictionary")
window.geometry("1000x700")

outputtxt=tk.Text(
    window,
    height=7,
    width=57,
    font=("Arial",14),
    bg="white",
    fg="black",
    borderwidth=3,

)
outputtxt.place(x=120,y=400)

myname=StringVar(window)
firstclick1=True


def clear_text():
    inputentry.delete(0,END)
    outputtxt.delete("1.0","end")


def on_inputentry_click(event):
    global firstclick1

    if firstclick1:
        firstclick1=False
        inputentry.delete(0,"end")

inputentry=Entry(window,font=("Arial",14),width=57,border=2,bg="white",fg="black")
inputentry.place(x=120,y=100)

Button(
  window,
  text="clear",
  command=clear_text,
  font=("Arial",20),
  bg="white",
  fg="black",
  borderwidth=3,
).place(x=770,y=250)

def equ_result():
    text=inputentry.get()
    if text=='':
        pass
    else:
        if '+' in text:
            location = text.index('+')
            num1 = int(text[0:location])
            num2 = int(text[location + 1:])
            result = num1 + num2
            text = str(result)

            outputtxt.insert(END,"reslut:"+text+'\n')
        elif '-' in text:
            location = text.index('-')
            num1 = int(text[0:location])
            num2 = int(text[location + 1:])
            result = num1 - num2
            text = str(result)

            outputtxt.insert(END, "reslut:" + text+'\n')
        elif '*' in text:
            location = text.index('*')
            num1 = int(text[0:location])
            num2 = int(text[location + 1:])
            result = num1 * num2
            text = str(result)

            outputtxt.insert(END, "reslut:" + text+'\n')
        elif '/' in text:
            location = text.index('/')
            num1 = int(text[0:location])
            num2 = int(text[location + 1:])
            result = num1 / num2
            text = str(result)

            outputtxt.insert(END, "reslut:" + text+'\n')





def exit_win():
    if mbox.askokcancel("Exit","Do you want to exit"):
        window.destroy()

keypad=Keypad(window)
keypad.show(inputentry)

window.protocol("WM_DELETE_WINDOW", exit_win)
window.mainloop()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-21 17:13:49 | 显示全部楼层
代码发到[code][/code]标签里
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2023-4-21 21:45:10 | 显示全部楼层
歌者文明清理员 发表于 2023-4-21 17:13
代码发到[code][/code]标签里

不常用啊,下次就懂了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-23 15:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表