鱼C论坛

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

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

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

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

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

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


  5. # -----------------------------------------------------------------------------------------------



  6. class Keypad(tk.Frame):


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

  10.         self.target = None
  11.         self.memory = ""

  12.         for x, item in enumerate(self.keyboardnum):
  13.             b = tk.Button(
  14.                 self,
  15.                 text=item,
  16.                 command=lambda text=item: self.append(text),
  17.                 font=("Arial", 14),
  18.                 bg="white",
  19.                 fg="black",
  20.                 borderwidth=3,
  21.                 relief="raised",
  22.             )
  23.             b.grid(row=(x%3), column=int(x/3), sticky="news")

  24.         x = tk.Button(
  25.             self,
  26.             text="+",
  27.             command=self.add_sym,
  28.             font=("Arial", 14),
  29.             bg="white",
  30.             fg="black",
  31.             borderwidth=3,
  32.             relief="raised",
  33.         )
  34.         x.grid(row=0, column=10, columnspan="2", sticky="news")

  35.         x = tk.Button(
  36.             self,
  37.             text="-",
  38.             command=self.dec_sym,
  39.             font=("Arial", 14),
  40.             bg="white",
  41.             fg="black",
  42.             borderwidth=3,
  43.             relief="raised",
  44.         )
  45.         x.grid(row=0, column=12, columnspan="2", sticky="news")

  46.         x = tk.Button(
  47.             self,
  48.             text="*",
  49.             command=self.mul_sym,
  50.             font=("Arial", 14),
  51.             bg="white",
  52.             fg="black",
  53.             borderwidth=3,
  54.             relief="raised",
  55.         )
  56.         x.grid(row=0, column=14, columnspan="3", sticky="news")

  57.         x = tk.Button(
  58.             self,
  59.             text="/",
  60.             command=self.div_sym,
  61.             font=("Arial", 14),
  62.             bg="white",
  63.             fg="black",
  64.             borderwidth=3,
  65.             relief="raised",
  66.         )
  67.         x.grid(row=0, column=17, columnspan="2", sticky="news")

  68.         x = tk.Button(
  69.             self,
  70.             text="=",
  71.             command=self.equ_sym,
  72.             font=("Arial", 14),
  73.             bg="white",
  74.             fg="black",
  75.             borderwidth=3,
  76.             relief="raised",
  77.         )
  78.         x.grid(row=0, column=19, columnspan="2", sticky="news")




  79.     def get(self):
  80.         if self.target:
  81.             return self.target.get()

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

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

  88.     def backspace(self):
  89.         if self.target:
  90.             text = self.get()
  91.             text = text[:-1]
  92.             self.clear()
  93.             self.append(text)

  94.     def space(self):
  95.         if self.target:
  96.             text = self.get()
  97.             text = text + " "
  98.             self.clear()
  99.             self.append(text)

  100.     def copy(self):
  101.         # TODO: copy to clipboad
  102.         if self.target:
  103.             self.memory = self.get()
  104.             self.label["text"] = "memory: " + self.memory
  105.             print(self.memory)

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

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



  113.     def add_sym(self):
  114.         if self.target:
  115.             text=self.get()
  116.             text=text+"+"
  117.             self.clear()
  118.             self.append(text)

  119.     def dec_sym(self):
  120.         if self.target:
  121.             text=self.get()
  122.             text=text+"-"
  123.             self.clear()
  124.             self.append(text)

  125.     def mul_sym(self):
  126.         if self.target:
  127.             text=self.get()
  128.             text=text+"*"
  129.             self.clear()
  130.             self.append(text)

  131.     def div_sym(self):
  132.         if self.target:
  133.             text=self.get()
  134.             text=text+"/"
  135.             self.clear()
  136.             self.append(text)

  137.     def equ_sym(self):
  138.         if self.target:
  139.             equ_result()
  140.             self.clear()


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

  144. outputtxt=tk.Text(
  145.     window,
  146.     height=7,
  147.     width=57,
  148.     font=("Arial",14),
  149.     bg="white",
  150.     fg="black",
  151.     borderwidth=3,

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

  154. myname=StringVar(window)
  155. firstclick1=True


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


  159. def on_inputentry_click(event):
  160.     global firstclick1

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

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

  166. Button(
  167.   window,
  168.   text="clear",
  169.   command=clear_text,
  170.   font=("Arial",20),
  171.   bg="white",
  172.   fg="black",
  173.   borderwidth=3,
  174. ).place(x=770,y=250)

  175. def equ_result():
  176.     text=inputentry.get()
  177.     if text=='':
  178.         pass
  179.     else:
  180.         if '+' in text:
  181.             location = text.index('+')
  182.             num1 = int(text[0:location])
  183.             num2 = int(text[location + 1:])
  184.             result = num1 + num2
  185.             text = str(result)

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

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

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

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





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

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

  213. window.protocol("WM_DELETE_WINDOW", exit_win)
  214. window.mainloop()











复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-4-21 17:13:49 | 显示全部楼层
代码发到[code][/code]标签里
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

不常用啊,下次就懂了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-28 23:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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