如何使我的计算器与tkinter计算所有的条目?

问题描述 投票:1回答:1

所以我想用tkinter做一个简单的计算器,但是遇到了一个问题,问题是我真的不知道如何让tkinter从条目中获取所有的东西,并将其计算出来,然后输出到同一个条目中。每次我试图将用户输入的所有内容保存在一个变量中(我通过尝试保存函数set_text中的文本来做到这一点),要么我得到一个名称错误= (NameError: name 'data_from_entry' is not defined)(甚至当我使用全局data_from_entry时),要么它只保存用户输入的最后一部分(我想发生这种情况是因为函数set_text中的文本在每次我点击另一个按钮时都会改变,所以最后它只有我使用的最后一个字符串)。

我也试过用eval计算,但也没有用。

所以这是我的代码,随时可以复制和修改任何你想要的东西,所有的技巧都欢迎。

'''from tkinter import *

root = Tk()
root.title('Calculator')
root.iconbitmap('abc.ico') #my icon

HEIGHT = 530
WIDTH = 325

canvas = Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()


frame1 = Frame(root, bg='#BEBDB8', bd=5)
frame1.place(relx=0.5, rely=0, relwidth=1, relheight=0.3, anchor='n')

entry1 = Entry(frame1, bg='#BEBDB8')
entry1.place(relx=0.5, rely=0, relwidth=0.97, relheight=0.97, anchor='n')
entry1.config(font=("Courier", 44))


def CombinedFunctions(): # i use this so i can give a button multiply commands
    set_text('')
    Result()


global text2 # now text2 is not a local variable anymore

def set_text(text):
    entry1.insert(END, text)
    text2 =''
    text2 = str(text) # tryna save the text in text2 so it's easier and so i make sure it's a string
global data # global again
data = ''
data = data + text2
def Result():
    print(str(data)) # here im using just print to check if the real problem is eval or just my bad #code skills
    #calculate = str(eval(str(data))) #here is where i tried eval
    #entry1.delete(0, END)
    #entry1.insert(0, calculate)







#global database2, database     

#def Database(text):                   # some other things i tried
#   database2 = ""
#   database2 = database2 + database  


def delete():
    entry1.delete(0, END)   # deleting strings with the delete button


frame2 = Frame(root, bg='#BEBDB8', bd=5)
frame2.place(relx=0, rely=0.3, relwidth=1, relheight=0.7)

button1 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=1, font=('Courier', 30), command=lambda: set_text(1))
button1.place(relx=0.015, rely=0, relwidth=0.225, relheight=0.150)

button2 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=2, font=('Courier', 30), command=lambda: set_text(2))
button2.place(relx=0.270, rely=0, relwidth=0.225, relheight=0.150)

button3 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=3, font=('Courier', 30), command=lambda: set_text(3))
button3.place(relx=0.525, rely=0, relwidth=0.225, relheight=0.150)

button_delete = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text='<-', font=('Courier', 30), command=lambda: delete())
button_delete.place(relx=0.77, rely=0, relwidth=0.225, relheight=0.150)

button4 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=4, font=('Courier', 30), command=lambda: set_text(4))
button4.place(relx=0.015, rely=0.160, relwidth=0.225, relheight=0.150)

button5 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=5, font=('Courier', 30), command=lambda: set_text(5))
button5.place(relx=0.270, rely=0.160, relwidth=0.225, relheight=0.150)

button6 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=6, font=('Courier', 30), command=lambda: set_text(6))
button6.place(relx=0.525, rely=0.160, relwidth=0.225, relheight=0.150)

button_multiply = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text='x', font=('Courier', 30), command=lambda: set_text('*'))
button_multiply.place(relx=0.77, rely=0.160, relwidth=0.105, relheight=0.150)

button_devide = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=':', font=('Courier', 30), command=lambda: set_text(':'))
button_devide.place(relx=0.89, rely=0.160, relwidth=0.105, relheight=0.150)

button7 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=7, font=('Courier', 30), command=lambda: set_text(7))
button7.place(relx=0.015, rely=0.320, relwidth=0.225, relheight=0.150)

button8 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=8, font=('Courier', 30), command=lambda: set_text(8))
button8.place(relx=0.270, rely=0.320, relwidth=0.225, relheight=0.150)

button9 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=9, font=('Courier', 30), command=lambda: set_text(9))
button9.place(relx=0.525, rely=0.320, relwidth=0.225, relheight=0.150)

button_add = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text='+', font=('Courier', 30), command=lambda: set_text('+'))
button_add.place(relx=0.77, rely=0.320, relwidth=0.105, relheight=0.150)

button_minus = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text='-', font=('Courier', 30), command=lambda: set_text('-'))
button_minus.place(relx=0.89, rely=0.320, relwidth=0.105, relheight=0.150)

button_date = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text='date', font=('Courier', 20), command=lambda: set_text('you forgot this'))
button_date.place(relx=0.015, rely=0.480, relwidth=0.225, relheight=0.150)

button0 = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=0, font=('Courier', 30), command=lambda: set_text(0))
button0.place(relx=0.270, rely=0.480, relwidth=0.225, relheight=0.150)

button_float = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text=',', font=('Courier', 30), command=lambda: set_text('.'))
button_float.place(relx=0.525, rely=0.480, relwidth=0.225, relheight=0.150)

button_result = Button(frame2, bg='#D9DDDC', relief='flat', activebackground='#BEBDB8', text='=', font=('Courier', 30), command=lambda: CombinedFunctions()) # here i call the combinedfunction which calls #other 2 functions
button_result.place(relx=0.77, rely=0.480, relwidth=0.225, relheight=0.150)'''

欢迎任何帮助:)

python variables tkinter eval
1个回答
0
投票

你应该使用eval()这样的表达式(里面有一个字符串)

x='56*78'
eval(x)

# 4368
© www.soinside.com 2019 - 2024. All rights reserved.