I'm making a silly calculator using Tkinter, and I have a global variable called "phrase". So basically, I have buttons (meaningless names), and I just want to add/subtract and print out sentences, such as "banana" + "milk" = "banana milk!" But I'm having difficulties saving user's inputs into global variable "phrase". Below is my code:
from tkinter import *
phrase = ''
# To press any button
def press(item):
global phrase
if item == 'Banana':
phrase = 'This is yellow'
elif item == 'Milk':
phrase = 'This is white'
return equation.set(phrase)
############################### Here is the fucntion adding together
def recipe(item):
global phrase
if item == 'AND':
phrase = phrase + ' ' + str(item)
equation.set(phrase)
# Driver code
if __name__ == '__main__':
# create application window
app = Tk()
# title
app.title("Silly Calculator")
# geometry
app.geometry('290x162')
# background color
app.configure(bg='pink')
equation = StringVar()
windows = Entry(app, textvariable=equation)
windows.grid(columnspan=5, ipadx=100, ipady=10)
equation.set('Listen to your Funculator!')
# Create buttons and other accessories
button1 = Button(app, text='Banana', fg='yellow', bg='purple',
command=lambda: press('Banana'), height=2, width=10)
button1.grid(row=2, column=0, sticky="NSEW")
button2 = Button(app, text='Milk', fg='brown', bg='pink',
command=lambda: press('Milk'), height=2, width=10)
button2.grid(row=2, column=1, sticky="NSEW")
plus = Button(app, text='AND', fg='black', bg='white',
command=lambda: recipe('AND'), height=2, width=10)
plus.grid(row=4, column=0, sticky="NSEW")
# start the GUI
app.mainloop()
So I tried to make the global variable phase to a list [], and maybe access by the index number. But that does not work, and I only get one last user-input saved to "phrase". Is there a way that I can save in different variables such as phrase_1, phrase_2 so that I can use these when:
# This is enter
def recipe(item):
global phrase
if item == 'AND':
phrase = phrase_1 + phrase_2
equation.set(phrase)
like this?
Any advice will be appreciated!!
Checkbuttonfor the recipients instead ofButton. Then you can check which recipients are checked and construct the required recipe.