如何在 python 中使用 TKINTER 进行退格
how to make a backspace with TKINTER in python
这是我的第一个应用程序,我正在尝试完成我的计算器项目,我想添加一个退格按钮,它可以删除输入框中的最后一个数字或文本
我做了一些研究,但我没有找到适用于我的代码的确切语法,我可以清除我的代码,但我不能只删除最后一个字符
from tkinter import *
root = Tk()
root.title("Calculator")
root.iconbitmap(r'C:\Users\iron\Downloads\favicon.ico')
canvas = Canvas(root,height = 378, width =378)
canvas.pack()
frame = Frame(root,bg = "#47FF98")
frame.place(relheight = 1 ,relwidth = 1)
#entry box
var = StringVar()
#to show text in screen
def press(x):
global a
a = a + str(x)
var.set(a)
# to show result
def result():
global a
total =(eval(a))
var.set(total)
def clear():
global a
a = ""
var.set("")
a = ""
e = Entry(frame,textvariable=var, width=20)
e.pack()
# = button
# equal button
equal = Button(frame,text = "=",activebackground = "#FFD700", bg = "red" , fg = "white",command = result)
equal.pack()
equal.place(relx = 0.8 ,rely = 0.9,relheight = 0.1 ,relwidth = 0.2)
label = Label(frame,relief="solid" ,text = "print anything here")
label.pack()
# plus button
plus = Button(frame,text = "+",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("+"))
plus.pack()
plus.place(relx = 0.8 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
# manipulat
manipulat = Button(frame,text = "x",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("*"))
manipulat.pack()
manipulat.place(relx = 0.8 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
#devide
devide = Button(frame,text = "/",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("/"))
devide.pack()
devide.place(relx = 0.8 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
#subtractions
subtractions = Button(frame,text = "-",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("-"))
subtractions.pack()
subtractions.place(relx = 0.8 ,rely = 0.5,relheight = 0.1 ,relwidth = 0.2)
#point
point= Button(frame,text = ".",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda : press("."))
point.pack()
point.place(relx = 0.6 ,rely = 0.9,relheight = 0.1 ,relwidth = 0.2)
#numbers
zero= Button(frame,text = "0",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda : press(0))
zero.pack()
zero.place(relx = 0.4 ,rely = 0.9,relheight = 0.1 ,relwidth = 0.2)
two= Button(frame,text = "2",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda : press(2))
two.pack()
two.place(relx = 0.4 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
five= Button(frame,text = "5",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(5))
five.pack()
five.place(relx = 0.4 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
eight= Button(frame,text = "8",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(8))
eight.pack()
eight.place(relx = 0.4 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
nine= Button(frame,text = "9",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(9))
nine.pack()
nine.place(relx = 0.6 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
six= Button(frame,text = "6",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(6))
six.pack()
six.place(relx = 0.6 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
three= Button(frame,text = "3",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(3))
three.pack()
three.place(relx = 0.6 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
one= Button(frame,text = "1",activebackground = "#FFD700",bg = "red",fg = "white",command=lambda :press(1))
one.pack()
one.place(relx = 0.2 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
four= Button(frame,text = "4",activebackground = "#FFD700",bg = "red",fg = "white",command=lambda: press(4))
four.pack()
four.place(relx = 0.2 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
seven= Button(frame,text = "7",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda: press(7))
seven.pack()
seven.place(relx = 0.2 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
clear= Button(frame,text="C",activebackground = "#FFD700",bg = "red",fg = "white",command=clear)
clear.pack
clear.place(relx = 0.6 ,rely = 0.5,relheight = 0.1 ,relwidth = 0.2)
root.mainloop()
按照您到目前为止的代码语法,您可以添加这样的函数
def backspace():
global a
a = a[:-1]
var.set(a)
a[:-1]
获取字符串 a
中除最后一个字符以外的所有字符
这应该有效
def backspace():
global a
a = a[:-1]
var.set(a)
如果这是你想要的,请告诉我
这是我的第一个应用程序,我正在尝试完成我的计算器项目,我想添加一个退格按钮,它可以删除输入框中的最后一个数字或文本 我做了一些研究,但我没有找到适用于我的代码的确切语法,我可以清除我的代码,但我不能只删除最后一个字符
from tkinter import *
root = Tk()
root.title("Calculator")
root.iconbitmap(r'C:\Users\iron\Downloads\favicon.ico')
canvas = Canvas(root,height = 378, width =378)
canvas.pack()
frame = Frame(root,bg = "#47FF98")
frame.place(relheight = 1 ,relwidth = 1)
#entry box
var = StringVar()
#to show text in screen
def press(x):
global a
a = a + str(x)
var.set(a)
# to show result
def result():
global a
total =(eval(a))
var.set(total)
def clear():
global a
a = ""
var.set("")
a = ""
e = Entry(frame,textvariable=var, width=20)
e.pack()
# = button
# equal button
equal = Button(frame,text = "=",activebackground = "#FFD700", bg = "red" , fg = "white",command = result)
equal.pack()
equal.place(relx = 0.8 ,rely = 0.9,relheight = 0.1 ,relwidth = 0.2)
label = Label(frame,relief="solid" ,text = "print anything here")
label.pack()
# plus button
plus = Button(frame,text = "+",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("+"))
plus.pack()
plus.place(relx = 0.8 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
# manipulat
manipulat = Button(frame,text = "x",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("*"))
manipulat.pack()
manipulat.place(relx = 0.8 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
#devide
devide = Button(frame,text = "/",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("/"))
devide.pack()
devide.place(relx = 0.8 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
#subtractions
subtractions = Button(frame,text = "-",activebackground = "#FFD700",bg = "red",fg = "white",command = lambda :press("-"))
subtractions.pack()
subtractions.place(relx = 0.8 ,rely = 0.5,relheight = 0.1 ,relwidth = 0.2)
#point
point= Button(frame,text = ".",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda : press("."))
point.pack()
point.place(relx = 0.6 ,rely = 0.9,relheight = 0.1 ,relwidth = 0.2)
#numbers
zero= Button(frame,text = "0",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda : press(0))
zero.pack()
zero.place(relx = 0.4 ,rely = 0.9,relheight = 0.1 ,relwidth = 0.2)
two= Button(frame,text = "2",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda : press(2))
two.pack()
two.place(relx = 0.4 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
five= Button(frame,text = "5",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(5))
five.pack()
five.place(relx = 0.4 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
eight= Button(frame,text = "8",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(8))
eight.pack()
eight.place(relx = 0.4 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
nine= Button(frame,text = "9",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(9))
nine.pack()
nine.place(relx = 0.6 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
six= Button(frame,text = "6",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(6))
six.pack()
six.place(relx = 0.6 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
three= Button(frame,text = "3",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda :press(3))
three.pack()
three.place(relx = 0.6 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
one= Button(frame,text = "1",activebackground = "#FFD700",bg = "red",fg = "white",command=lambda :press(1))
one.pack()
one.place(relx = 0.2 ,rely = 0.8,relheight = 0.1 ,relwidth = 0.2)
four= Button(frame,text = "4",activebackground = "#FFD700",bg = "red",fg = "white",command=lambda: press(4))
four.pack()
four.place(relx = 0.2 ,rely = 0.7,relheight = 0.1 ,relwidth = 0.2)
seven= Button(frame,text = "7",activebackground = "#FFD700",bg = "red",fg = "white",command =lambda: press(7))
seven.pack()
seven.place(relx = 0.2 ,rely = 0.6,relheight = 0.1 ,relwidth = 0.2)
clear= Button(frame,text="C",activebackground = "#FFD700",bg = "red",fg = "white",command=clear)
clear.pack
clear.place(relx = 0.6 ,rely = 0.5,relheight = 0.1 ,relwidth = 0.2)
root.mainloop()
按照您到目前为止的代码语法,您可以添加这样的函数
def backspace():
global a
a = a[:-1]
var.set(a)
a[:-1]
获取字符串 a
这应该有效
def backspace():
global a
a = a[:-1]
var.set(a)
如果这是你想要的,请告诉我