【问题标题】:python tkinter calculator projectpython tkinter 计算器项目
【发布时间】:2021-03-25 21:34:46
【问题描述】:

我在大学的第一年,我开始学习 Python。我计划使用 tkinter 从 python 制作一个计算器。我已经有了 GUI 的代码,但我的问题在于删除功能。当我使用它时,它会重新初始化小部件条目中的文本变量,但是当我按下一个键之后,前面的表达式重新出现并与使用删除之前的表达式连接。就好像整体operator变量没有被重新初始化……请问我该如何解决这个问题?

这是完整的代码

from tkinter import * 


def expression(nombres):
   global operateur
   operateur = operateur + str(nombres)
   text_input.set(operateur)

def egal():
   pass

def q():
   racine.destroy()

def delete():
   ecran.delete(0, END)
   return;


if __name__ == '__main__':
 racine = Tk()
 operateur = ""
 text_input = StringVar()


   #boutons numéraires

 bouton1 = Button(racine, text = " 1 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(1))          
 bouton1.grid(column = 0, row = 3) 

 bouton2 = Button(racine, text = " 2 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(2))          
 bouton2.grid(column = 1, row = 3) 

 bouton3 = Button(racine, text = " 3 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(3))
 bouton3.grid(column = 2, row = 3) 

 bouton4 = Button(racine, text = " 4 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(4))
 bouton4.grid(column = 0, row = 2) 

 bouton5 = Button(racine, text = " 5 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(5))
 bouton5.grid(column = 1, row = 2) 

 bouton6 = Button(racine, text = " 6 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(6)) 
 bouton6.grid(column = 2, row = 2) 

 bouton7 = Button(racine, text = " 7 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(7))
 bouton7.grid(column = 0, row = 1) 

 bouton8 = Button(racine, text = " 8 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(8))
 bouton8.grid(column = 1, row = 1) 

 bouton9 = Button(racine, text = " 9 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(9))
 bouton9.grid(column = 2, row = 1)

 ecran = Entry(racine, text = "Saisir...", width = 30, textvariable = text_input)
 ecran.grid(column =0, row = 0, columnspan = 5)

 bouton0 = Button(racine, text = " 0 ", activebackground = "black", height = 2, width = 4,
                  bg = "gray69", command = lambda:expression(0))
 bouton0.grid(column = 1, row = 4)

 blankbutton = Button(racine, text = "   ", state = 'disabled', height = 2, width = 4,
                      bg = "gray69")
 blankbutton.grid(column = 0, row = 4)

 virg = Button(racine, text = " . ", activebackground = "black", height = 2, width = 4,
                bg = "gray69", command = lambda:expression("."))
 virg.grid(column = 2, row = 4)

 plus = Button(racine, text = " + ", activebackground = "gray90",
               height = 2, width = 4, bg = "gray50", command = lambda:expression("+"))
 plus.grid(column = 3, row = 1)

 minus = Button(racine, text = " - ", activebackground = "gray90",
               height = 2, width = 4, bg = "gray50", command = lambda:expression("-"))
 minus.grid(column = 3, row = 2)

 mul = Button(racine, text = " x ", activebackground = "gray90",
             height = 2, width = 4, bg = "gray50", command = lambda:expression("x"))
 mul.grid(column = 3, row = 3)

 div = Button(racine, text = " / ", activebackground = "gray90",
                    height = 2, width = 4, bg = "gray50", command = lambda:expression("/"))
 div.grid(column = 3, row = 4)
   
 equal = Button(racine, text = " = ", activebackground = "gray90", height = 1, width = 4,
                bg = "gray50")
 equal.grid(column = 3, row = 5)

 supr = Button(racine, text = " C ", activebackground = "gray90", height = 2, width = 4,
                bg = "gray50", command = delete)
 supr.grid(column = 4, row = 1)

 quitter = Button(racine, text = "quitter", command = q, height =1, width = 15)
 quitter.grid(column = 0, row = 5, columnspan = 3)


 racine.title( "Calculatrice") 
 racine.configure(bg = "gray10")
 racine.mainloop()

【问题讨论】:

  • 请将您的代码作为文本添加到问题中。不是图片。
  • 等一下

标签: python tkinter project calculator


【解决方案1】:

您还需要更新operateur,使用text_input更新条目更容易:

def delete():
   global operateur
   operateur = ""
   text_input.set(operateur)

【讨论】:

    猜你喜欢
    • 2021-03-27
    • 2016-12-09
    • 2015-09-25
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 2017-11-05
    • 2013-11-24
    • 2015-09-29
    相关资源
    最近更新 更多