[英]How can I get input from multiple entry widgets created in a loop?
我已经搜索了之前的一些答案并更近了一步,但我的问题仍然是我无法从多个输入框中获取值。
import tkinter as tk
from tkinter import ttk
window = tk.Tk()
my_list = []
def get_info():
for each_player in my_list:
tk.Label(window, text=temp_entry.get()).grid()
#number of players is determined by the user.
#In this example, lets say there are 3 players
tk.Label(window, text="Number of Players: ").grid()
num_of_players = ttk.Combobox(window, values=[1, 2, 3])
num_of_players.current(2)
num_of_players.grid(row=0, column=1)
#The code above is only the recreate the user selecting the amount of players from a combobox
#create number of entry boxes for user-determined number of players
for each_player in range(1, int(num_of_players.get()) + 1):
temp_label = tk.Label(window, text="Player {}: ".format(each_player))
temp_entry = tk.Entry(window)
my_list.append(temp_entry)
temp_label.grid(row=each_player, column=0, pady=10)
temp_entry.grid(row=each_player, column=1, pady=10)
button = tk.Button(window, text="Save", command=get_info)
button.grid()
window.mainloop()
在代码的末尾,我努力找出如何从输入框中获取信息。 我如何才能使用 get() 方法,但只能在用户输入文本之后使用?
您的列表包含条目小部件,因此在您的循环中您需要引用循环变量而不是temp_entry
:
def get_info():
for each_player in my_list:
print(each_player.get())
使用一个按钮来触发一个为你获取文本的方法,比如:
play = Button(window, text="get_button", command=that_getter_method)
.....
.....
def that_getter_method():
var = field.get()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.