我正在尝试使用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被覆盖,或者是否有其他方法可以做到这一点?你知道吗
编辑aaaaantone
我正要给你澄清我想要什么,但我刚刚发现我做错了什么,谢谢你的帮助,你教了我什么。加入,谢谢。你知道吗
除了在
script()
的开头之外,您的代码从未实际分配给itemslist
。它唯一出现在赋值运算符左侧的时间是在初始化时。你知道吗您可能可以将
itemslist1
的所有实例更改为itemslist
,并拥有一个工作程序。你知道吗编辑
经过进一步审查,我怀疑你认为str.join公司(v) 将字符串
v
附加到str。join不是这样工作的。你知道吗join
将列表作为参数并将其内容连接在一起,str
实例作为分隔符。通常,源字符串实际上是一个空字符串或逗号。你知道吗那我该怎么做呢?
使用以下语法附加到字符串:
(或速记版本:)
相关问题 更多 >
编程相关推荐