0

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!!

2
  • 1
    You can use Checkbutton for the recipients instead of Button. Then you can check which recipients are checked and construct the required recipe. Commented Apr 8, 2020 at 1:59
  • Thank you, I didn't know about Check button....!! Commented Apr 8, 2020 at 2:01

1 Answer 1

1

Something like this? Or I didn't understand correctly?

from tkinter import *

phrase = []
phrase_string = ''


# To press any button
def press(item):
    global phrase_string
    global phrase

    if item == 'Banana':
        phrase.append(' Banana')
    elif item == 'Milk':
        phrase.append(' Milk')
    elif item == 'AND':
        phrase.append(' and')

    phrase_string = ''
    for ele in phrase:
        phrase_string += ele

    equation.set(phrase_string)



# 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)

    # 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: press('AND'), height=2, width=10)
    plus.grid(row=4, column=0, sticky="NSEW")

    app.mainloop()
Sign up to request clarification or add additional context in comments.

9 Comments

Ohh it seems like I can do that way. But the user input shows in []. Can I get rid of []? And just get the text out of it?
I edited the code and added how to convert a list phrase = [] to string phrase_string = ''.
Thank you so much!!! I changed my code according to yours and actually I understand so much better now.
I am glad to hear that. Add a comment, if you will have any more concerns.
Hi I have another question! So, I want to make every button to be only able to click once, so for example, no double digits such as 12, 13 but just 1 + 3 and so on.. What kind of function can it do this? Since my calculator has strings, like banana and milk, I don't wnat to type (banana banana + milk) but instead only (banana + milk).. if you can understand me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.