在 python 中的整个程序中保留一个字符串而不覆盖它

Keeping a string without over writing it throughout the program in python

我正在尝试制作一个程序来存储用户的食谱,使用 tkinter gui 来实现。我需要想办法跟踪输入的内容,并将其存储在文本文件中。我尝试使用列表无济于事,并认为使用字符串是前进的方向,但遇到了一个问题 - 每次我尝试添加到字符串时,它都会覆盖并且不会保留之前的数据。我试过使用

mystring.join(a + b + etc)

但是没有用,我的新代码如下:

from tkinter import *

number_people = 1
itemslist = ''
itemslist1 = ''

def script ():   # Puts main body of program into a function so that it can be re-run #

    global number_people
    number_people = 1

    global itemslist, itemslist1
    itemslist = ''
    itemslist1 = ''

    #### MAIN ####

    fake_window = Tk()                          #                                   #                             
    new_recipe_window = fake_window             # Opens window, allows it be closed #
    start_window = fake_window                  #                                   #
    start_window.title("Recipe Book Task")      #                                   #


    #### MAIN ####  



    ### Functions ###

    def close (x):

        global start_window
        global new_recipe_window
        (x).withdraw()

    def moreitems ():

        a = item_box.get()
        b = quantity_units_box.get()
        c = len(a)
        if a == '':
            pass

        elif b == '':
            pass

        else:
            item_box.delete(0,c)
            quantity_units_box.delete(0,c)
            global itemslist
            global itemslist1
            itemslist1 = itemslist + a + ', ' + b + ', '
            print ("Items list =", itemslist1)


    def new_recipe ():

        new_recipe_window = Tk()
        new_recipe_window.title("New Recipe")
        close(start_window)

        recipe_name_label = Label(new_recipe_window, text="Recipe Name: ")
        recipe_name_label.grid(row=0, column=0)

        recipe_name_box = Entry(new_recipe_window)
        recipe_name_box.grid(row=0, column=1)

        def continue_1 ():

            global check_box
            check_box = recipe_name_box.get()
            if check_box == '':
                pass

            else:
                global itemslist
                global itemslist1
                itemslist1 = itemslist + check_box + ', '
                print (itemslist1)
                continue_button_1.destroy()

                item_label = Label(new_recipe_window, text="Ingredient: ")
                item_label.grid(row=1, column=0)

                global item_box
                item_box = Entry(new_recipe_window)
                item_box.grid(row=1, column=1)

                quantity_units_label = Label(new_recipe_window, text="Quantity and Units: ")
                quantity_units_label.grid(row=2, column=0)

                global quantity_units_box
                quantity_units_box = Entry(new_recipe_window)
                quantity_units_box.grid(row=2, column=1)

                def continue_2 ():
                    check_box_1 = item_box.get()
                    check_box_2 = quantity_units_box.get()
                    if check_box_1 == '':
                        pass

                    elif check_box_2 == '':
                        pass

                    else:
                        global itemslist
                        itemslist.join(check_box_1)
                        itemslist.join(check_box_2)

                        continue_button_2.destroy()
                        more_items.destroy()

                        add_people_label = Label(new_recipe_window, text="Choose amount of people")
                        add_people_label.grid(row=3, column=0, columnspan=2)

                        def add ():
                            global number_people
                            number_people += 1
                            num_people_label.config(text="Number of people: " + str(number_people))

                        def minus ():
                            global number_people
                            if number_people > 1:
                                number_people -= 1
                                num_people_label.config(text="Number of people: " + str(number_people))

                        def finish ():
                            itemslist.join(str(number_people))
                            print("ItemsList = " + itemslist)
                            saveFile = open("Recipe_Book.txt", "a")
                            saveFile.write(itemslist + '\n')
                            saveFile.close
                            close(new_recipe_window)
                            script()

                        num_people_label = Label(new_recipe_window, text="Number of people: " + str(number_people))
                        num_people_label.grid(row=4, column=0, columnspan=2)

                        add_people_button = Button(new_recipe_window, text="+")
                        add_people_button.grid(row=5, column=1)
                        add_people_button.config(command=add)

                        minus_people_button = Button(new_recipe_window, text="-")
                        minus_people_button.grid(row=5, column=0)
                        minus_people_button.config(command=minus)

                        finish_button = Button(new_recipe_window, text="Finish")
                        finish_button.grid(row=6, column=0, columnspan=2)
                        finish_button.config(command=finish)


                continue_button_2 = Button(new_recipe_window, text="Continue...")
                continue_button_2.grid(row=3, column=0)
                continue_button_2.config(command=continue_2)

                more_items = Button(new_recipe_window, text="Add another item", command=moreitems)
                more_items.grid(row=3, column=1)



        continue_button_1 = Button(new_recipe_window, text="Continue...")
        continue_button_1.grid(row=1, column=0)
        continue_button_1.config(command=continue_1)


    new_recipe = Button(start_window, text="New Recipe", command=new_recipe)
    new_recipe.grid(row=0, column=0)


script()

总而言之,我的问题是如何防止字符串 itemslist 和 itemslist1 被覆盖,或者有其他方法可以做到这一点吗?

为AAANTOINE编辑

我正要为你澄清我想要什么,但我只是弄清楚我做错了什么,感谢你的帮助,你教我.join 的作用,谢谢。

除了 script() 的开头之外,您的代码从未实际分配给 itemslist。它唯一一次出现在赋值运算符的左侧是在初始化时。

您可能可以将 itemslist1 的所有实例更改为 itemslist 并拥有一个工作程序。

编辑

经过进一步审查,我怀疑您认为 str.join(v) 将字符串 v 附加到 str。这不是 join 的工作方式。

>>> s = 'something'
>>> s.join('a')
'a'

join 将列表作为参数并将其内容连接在一起,str 实例作为分隔符。通常,源字符串实际上是空字符串或逗号。

>>> s.join(['a', 'b', 'c'])
'asomethingbsomethingc'
>>> ','.join(['a', 'b', 'c'])  # comma separation
'a,b,c'
>>> '-'.join(s)    # spell it out!
's-o-m-e-t-h-i-n-g'

那我该怎么做呢?

您使用以下语法附加到字符串:

>>> s = s + 'a'
>>> s
'somethinga'

(或 shorthand 版本:)

>>> s += 'a'
>>> s
'somethinga'