我正在Python 3上使用tkinter制作计算器。我已经完成了布局。现在,我试图获取它,以便当我按下按钮时,它会显示在输入框中。例如,如果我先按2按钮,再按+按钮,再按3按钮,则希望它显示在程序顶部的输入框中。
import tkinter
import tkinter.messagebox
class myGUI:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title('Calculator')
## Font
button_font = ('Verdana', 15)
entry_font = ('Verdana', 36)
## Define the frames
self.frameZero = tkinter.Frame(self.main_window)
self.frameOne = tkinter.Frame(self.main_window)
self.frameTwo = tkinter.Frame(self.main_window)
self.frameThree = tkinter.Frame(self.main_window)
self.frameFour = tkinter.Frame(self.main_window)
self.frameFive = tkinter.Frame(self.main_window)
## Define the buttons
self.my_button1 = tkinter.Button(self.frameOne, text='1', command = lambda:btnClick(my_button1), height = 3, width = 7, font = button_font)
self.my_button2 = tkinter.Button(self.frameOne, text='2', command = lambda:btnClick(my_button2), height = 3, width = 7, font = button_font)
self.my_button3 = tkinter.Button(self.frameOne, text='3', command = lambda:btnClick(my_button3), height = 3, width = 7, font = button_font)
self.my_buttonadd = tkinter.Button(self.frameOne, text='+', command = lambda:btnClick(my_buttonadd), height = 3, width = 7, font = button_font)
self.my_button4 = tkinter.Button(self.frameTwo, text='4', command = lambda:btnClick(my_button4), height = 3, width = 7, font = button_font)
self.my_button5 = tkinter.Button(self.frameTwo, text='5', command = lambda:btnClick(my_button5), height = 3, width = 7, font = button_font)
self.my_button6 = tkinter.Button(self.frameTwo, text='6', command = lambda:btnClick(my_button6), height = 3, width = 7, font = button_font)
self.my_buttonsub = tkinter.Button(self.frameTwo, text='-', command = lambda:btnClick(my_buttonsub), height = 3, width = 7, font = button_font)
self.my_button7 = tkinter.Button(self.frameThree, text='7', command = lambda:btnClick(my_button7), height = 3, width = 7, font = button_font)
self.my_button8 = tkinter.Button(self.frameThree, text='8', command = lambda:btnClick(my_button8), height = 3, width = 7, font = button_font)
self.my_button9 = tkinter.Button(self.frameThree, text='9', command = lambda:btnClick(my_button9), height = 3, width = 7, font = button_font)
self.my_buttonmult = tkinter.Button(self.frameThree, text='*', command = lambda:btnClick(my_buttonmult), height = 3, width = 7, font = button_font)
self.my_buttonC = tkinter.Button(self.frameFour, text='C', height = 3, width = 7, font = button_font)
self.my_button0 = tkinter.Button(self.frameFour, text='0', command = lambda:btnClick(my_button0), height = 3, width = 7, font = button_font)
self.my_buttonperiod = tkinter.Button(self.frameFour, text='.', command = lambda:btnClick(my_buttonperiod), height = 3, width = 7, font = button_font)
self.my_buttondiv = tkinter.Button(self.frameFour, text='/', command = lambda:btnClick(my_buttondiv), height = 3, width = 7, font = button_font)
self.my_buttoncalc = tkinter.Button(self.frameFive, text='Calculate', height = 4, width = 30, font = button_font)
## Define the entry area
self.my_entry = tkinter.Entry(self.frameZero, width = 13, font = entry_font)
## Pack the entry area
self.my_entry.pack(side = 'left')
## Pack the buttons
self.my_button1.pack(side = 'left')
self.my_button2.pack(side = 'left')
self.my_button3.pack(side = 'left')
self.my_buttonadd.pack(side = 'left')
self.my_button4.pack(side = 'left')
self.my_button5.pack(side = 'left')
self.my_button6.pack(side = 'left')
self.my_buttonsub.pack(side = 'left')
self.my_button7.pack(side = 'left')
self.my_button8.pack(side = 'left')
self.my_button9.pack(side = 'left')
self.my_buttonmult.pack(side = 'left')
self.my_buttonC.pack(side = 'left')
self.my_button0.pack(side = 'left')
self.my_buttonperiod.pack(side = 'left')
self.my_buttondiv.pack(side = 'left')
self.my_buttoncalc.pack(side = 'left')
## Pack the frames
self.frameZero.pack()
self.frameOne.pack()
self.frameTwo.pack()
self.frameThree.pack()
self.frameFour.pack()
self.frameFive.pack()
## Click button function
def btnClick(numbers):
global operator
operator=operator + str(numbers)
my_entry = self.set(operator)
tkinter.mainloop()
my_gui = myGUI()
答案 0 :(得分:1)
您需要实现一个tk.StringVar()
,该值可以将值存储在计算器屏幕的顶部。要知道的主要方法是此变量的.get()
和.set()
,这就是访问值(.get()
)或更新值(.set()
)的方式。您必须使用textvariable
参数将其链接到条目。完成设置后,我将btnClick()
函数的输入全部更改为字符串。有了所需的字符串(例如“ 12 + 5”)后,就可以使用python内置的eval()
函数来计算结果了!请注意,此方法并不安全,因为用户可以根据实现的方式使用它执行任意代码(入侵程序)!您的代码的有效版本如下:
import tkinter
import tkinter.messagebox
class myGUI:
def __init__(self):
self.main_window = tkinter.Tk()
self.main_window.title('Calculator')
## Font
button_font = ('Verdana', 15)
entry_font = ('Verdana', 36)
## Define the frames
self.frameZero = tkinter.Frame(self.main_window)
self.frameOne = tkinter.Frame(self.main_window)
self.frameTwo = tkinter.Frame(self.main_window)
self.frameThree = tkinter.Frame(self.main_window)
self.frameFour = tkinter.Frame(self.main_window)
self.frameFive = tkinter.Frame(self.main_window)
## Define the entry area
self.entry_variable = tkinter.StringVar()
self.my_entry = tkinter.Entry(self.frameZero, textvariable=self.entry_variable, width=13, font=entry_font)
## Define the buttons
self.my_button1 = tkinter.Button(self.frameOne, text='1', command = lambda:btnClick("1"), height = 3, width = 7, font = button_font)
self.my_button2 = tkinter.Button(self.frameOne, text='2', command = lambda:btnClick("2"), height = 3, width = 7, font = button_font)
self.my_button3 = tkinter.Button(self.frameOne, text='3', command = lambda:btnClick("3"), height = 3, width = 7, font = button_font)
self.my_buttonadd = tkinter.Button(self.frameOne, text='+', command = lambda:btnClick("+"), height = 3, width = 7, font = button_font)
self.my_button4 = tkinter.Button(self.frameTwo, text='4', command = lambda:btnClick("4"), height = 3, width = 7, font = button_font)
self.my_button5 = tkinter.Button(self.frameTwo, text='5', command = lambda:btnClick("5"), height = 3, width = 7, font = button_font)
self.my_button6 = tkinter.Button(self.frameTwo, text='6', command = lambda:btnClick("6"), height = 3, width = 7, font = button_font)
self.my_buttonsub = tkinter.Button(self.frameTwo, text='-', command = lambda:btnClick("-"), height = 3, width = 7, font = button_font)
self.my_button7 = tkinter.Button(self.frameThree, text='7', command = lambda:btnClick("7"), height = 3, width = 7, font = button_font)
self.my_button8 = tkinter.Button(self.frameThree, text='8', command = lambda:btnClick("8"), height = 3, width = 7, font = button_font)
self.my_button9 = tkinter.Button(self.frameThree, text='9', command = lambda:btnClick("9"), height = 3, width = 7, font = button_font)
self.my_buttonmult = tkinter.Button(self.frameThree, text='*', command = lambda:btnClick("*"), height = 3, width = 7, font = button_font)
self.my_buttonC = tkinter.Button(self.frameFour, text='C', height = 3, width = 7, font = button_font)
self.my_button0 = tkinter.Button(self.frameFour, text='0', command = lambda:btnClick("0"), height = 3, width = 7, font = button_font)
self.my_buttonperiod = tkinter.Button(self.frameFour, text='.', command = lambda:btnClick("."), height = 3, width = 7, font = button_font)
self.my_buttondiv = tkinter.Button(self.frameFour, text='/', command = lambda:btnClick("/"), height = 3, width = 7, font = button_font)
self.my_buttoncalc = tkinter.Button(self.frameFive, text='Calculate', height = 4, width = 30, font = button_font, command=self.calculate)
## Pack the entry area
self.my_entry.pack(side = 'left')
## Pack the buttons
self.my_button1.pack(side = 'left')
self.my_button2.pack(side = 'left')
self.my_button3.pack(side = 'left')
self.my_buttonadd.pack(side = 'left')
self.my_button4.pack(side = 'left')
self.my_button5.pack(side = 'left')
self.my_button6.pack(side = 'left')
self.my_buttonsub.pack(side = 'left')
self.my_button7.pack(side = 'left')
self.my_button8.pack(side = 'left')
self.my_button9.pack(side = 'left')
self.my_buttonmult.pack(side = 'left')
self.my_buttonC.pack(side = 'left')
self.my_button0.pack(side = 'left')
self.my_buttonperiod.pack(side = 'left')
self.my_buttondiv.pack(side = 'left')
self.my_buttoncalc.pack(side = 'left')
## Pack the frames
self.frameZero.pack()
self.frameOne.pack()
self.frameTwo.pack()
self.frameThree.pack()
self.frameFour.pack()
self.frameFive.pack()
## Click button function
def btnClick(number_clicked):
self.entry_variable.set("{}{}".format(self.entry_variable.get(), number_clicked))
def calculate(self):
print("{}".format(eval(str(self.entry_variable.get()))))
self.entry_variable.set("{}".format(str(eval(str(self.entry_variable.get())))))
my_gui = myGUI()
my_gui.main_window.mainloop() # this should NOT be inside your class like you had it...
总体而言,我喜欢您的框架布局和整体设计。 Lambda函数也很好!欢迎使用StackOverflow。