kno 发表于 2023-4-21 10:59:25

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

本帖最后由 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=
    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)
            num2 = int(text)
            result = num1 + num2
            text = str(result)

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

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

            outputtxt.insert(END, "reslut:" + text+'\n')
      elif '/' in text:
            location = text.index('/')
            num1 = int(text)
            num2 = int(text)
            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()











歌者文明清理员 发表于 2023-4-21 17:13:49

代码发到[code][/code]标签里

kno 发表于 2023-4-21 21:45:10

歌者文明清理员 发表于 2023-4-21 17:13
代码发到标签里

不常用啊,下次就懂了
页: [1]
查看完整版本: 看了github的例子,自己写了个计算器,很简陋,只能进行简单的一次运算,可自己补充