[英]Only stores last input? Tkinter
I want the user to type in the amount of sold tickets for every cinema.我希望用户输入每个电影院的售票数量。 However it is only the last input that is stored in my variable "sålda".
然而,它只是存储在我的变量“sålda”中的最后一个输入。 How can I fix this?
我怎样才能解决这个问题?
def part_of_function():
bio1 = []
i = 0
for elements in BIOGRAFVAL:
vuxen = StringVar()
pensionär = StringVar()
barn = StringVar()
Label(fönster4, text= BIOGRAFVAL[i]).pack()
Label(fönster4, text="Skriv in antal biljetter av varje sort du vill köpa").pack()
Label(fönster4, text="Max antal platser för " + BIOGRAFVAL[i] + " är " + str(PLATSER[i])).pack()
personer = [('vuxen',vuxen),('pensionär', pensionär), ('barn',barn)]
for p in personer:
sålda = välj_biograf(fönster4, p[0] + "biljett", p[1]) #välj_biograf is called*
sålda.pack()
i += 1
Button(fönster4, text="fortsätt", command=lambda: info()).pack()
def info():
fönster5=Tk()
fönster5.title("Biljettförsäljning för EN biograf.")
fönster5.geometry("350x200")
bio1.append(sålda.get())
print(bio1) #here is only the last element printed
""" *code for the function "välj_biograf" that is called
def välj_biograf(fönster, person_typ, text_variabel):
Label(fönster, text=person_typ).pack()
sålda = Entry(fönster, textvariable=text_variabel)
sålda.pack()
return sålda """
bio1
is a local variable inside the part_of_function
function. bio1
是part_of_function
function 中的一个局部变量。 The value of it will be lost each time the function ends and reset back to a blank list again next time the function is called.每次 function 结束时它的值都会丢失,并在下次调用 function 时再次重置为空白列表。
Create the bio1
empty list outside the function (to make it global) and then use global bio
inside your function so that the global variable is modified.在 function 之外创建
bio1
空列表(使其成为全局),然后在 function 中使用global bio
以便修改全局变量。
Also note that you should only ever have one instance of Tk()
inside your code.另请注意,您的代码中应该只有一个
Tk()
实例。 I suspect that in your code you have more than one.我怀疑在你的代码中你有不止一个。 If you want a new window, use
Toplevel()
instead如果您想要一个新的 window,请改用
Toplevel()
As pointed out by CoolCloud, you need to use.get on the values of personer
/ p
正如CoolCloud所指出的,您需要使用.get来获取
personer
/ p
的值
sålda = välj_biograf(fönster4, p[0] + "biljett", p[1].get())
Element 1 of the tuple p
is a StringVar so you need to use .get()
to get it's value.元组
p
的元素 1 是 StringVar,因此您需要使用.get()
来获取它的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.