简体   繁体   English

如何将多个用户输入保存到 Python tkinter 中的变量中?

[英]How can I save multiple user inputs into variables in Python tkinter?

I'm making a silly calculator using Tkinter, and I have a global variable called "phrase".我正在使用 Tkinter 制作一个愚蠢的计算器,并且我有一个名为“短语”的全局变量。 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.所以我尝试将全局变量 phase 设置为列表 [],并可能通过索引号访问。 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:有没有一种方法可以保存在不同的变量中,例如短语_1、短语_2,以便我可以在以下情况下使用它们:

# 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!!任何建议将被认真考虑!!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 Python Tkinter 的输入保存到 txt 文件中? - How can I save inputs from Python Tkinter into a txt file? 如何在tkinter python中将多个文本字段输入及其功能连接到一个按钮? - How can I connect multiple text field inputs along with their functions to one button in tkinter python? 如何从用户那里获取多个(大量)输入并在用户提供的每个输入中重复代码? (Python) - How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python) 如何将多个输入保存到python中的txt文件 - how do i save multiple inputs to a txt file in python 将多个用户输入添加到字典中并将其写入到CSV,Python Tkinter中 - Adding multiple user inputs to a dictionary and writing them to csv, python tkinter Python:使用for循环时如何存储多个用户输入? - Python: How can I store multiple user inputs when using for loop? 如何将用户输入保存到列表Python - How to save user inputs into a list python 如何在计时器上的Tkinter GUI中刷新多个变量的内容? - How can I refresh the contents of multiple variables in a Tkinter GUI on a timer? 如何在 Python 中将用户输入格式化为变量 - How to Format User Inputs Into Variables in Python 如何在python中获得多个用户输入? - How do I get multiple user inputs in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM