gpt4 book ai didi

python - 有没有办法在 tkinter 中使用字符串作为输入框的变量名?

转载 作者:太空宇宙 更新时间:2023-11-03 21:15:42 25 4
gpt4 key购买 nike

我正在尝试使用 python/tkinter 制作一个应用程序,允许您为曲棍球队创建名单。我想根据团队中球员的参数数量生成“new_roster”框架,从而设法为每个球员创建可变数量的输入框,但无法从所述输入框中提取值。

在我当前的代码中,通过向空字符串添加“z”,根据用户希望加入其团队的玩家数量生成一个列表。然后下一个函数遍历列表,设置 string = Entry(frame, xxx),我最初尝试使用 int,但这似乎并不重要。我还尝试将 str 设为 StringVar 但也不起作用。

每次我尝试在创建后引用输入框,例如

a = (player_list[0])
b = a.get

我收到以下错误:

错误:“str”对象没有属性“get”

有点长,所以:https://repl.it/@masoneischens/StableToughTheories

重要部分:

我非常复杂的列表创建循环(entry2 = 团队中的玩家数量)

  for i in range(int(entry2.get())):
a = "z"
for u in range(i):
a += "z"
player_list.insert(i - 1, a)

实际创建输入框的循环(pr = 适当的行) 对于player_list中的n:

    pr += 1
a= Entry(edit_roster, width = 15)
a.grid(row = pr, column =3)

将值保存到 csv 文件的函数(fn = 文件名,s = 名称,不含“.csv”)

def save_new_file(s):
global player_list
fn = str(s) + ".csv"
with open(fn, mode='w') as roster_file:
roster_writer = csv.writer(roster_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
for i in player_list:
b = i.get()
roster_writer.writerow([b, "player 1 "])

基本上,我需要能够使用字符串作为名称来创建和引用变量,或者以某种方式将字符串转换为正确的变量名称,以便我可以从使用所述字符串创建的输入框中提取值。

最佳答案

发生错误是因为您正在对字符串调用get()方法。不要在 player_list 上调用 get() 方法,而是执行以下操作:

  1. 创建一个entry_list

    global entry_list
    entry_list = []
  2. 将条目插入条目列表

    entry_list.append(a)
  3. entry_list 中的条目调用 get() 方法

    global entry_list
    for i in entry_list:
    print(i.get())

这是您的代码的更新版本。这里我只是打印了条目中的值:

from tkinter import *
from tkinter import messagebox
from tkinter import simpledialog
import csv
import random

sample = Tk()
sample.geometry("500x500")


# lists


# functions
def switch_newroster():
new_roster.tkraise()


def switch_startpage():
start_page.tkraise()


def switch_loadroster():
load_roster.tkraise()


def get_file():
if entry_getfile_act.get() == "":
messagebox.showerror("ERROR!!", "Please enter a file name")
else:
print("good job")


def create():
global player_list

# ADD_1 Create new global entry_list #####
global entry_list
entry_list = []

player_list = []
number_list = []

pos_list = []

pr = 3
nr = 3
gr = 3
posr = 3
ar = 3
gaar = 3
if entry2.get() == "":
messagebox.showerror("ERROR!!", "Enter number of players")
return False
elif entry1.get() == "":
messagebox.showerror("ERROR!!", "Please enter a team name")
return False
elif int(entry2.get()) > 35 or int(entry2.get()) <= 0:
messagebox.showerror("ERROR!!", "Roster can contain 1-35 players")
else:
o = "we coolin my dude"
for i in range(int(entry2.get())):
a = "z"
for u in range(i):
a += "z"
player_list.insert(i - 1, a)
for i in range(int(entry2.get())):
a = i + 1
number_list.insert(i - 1, a)
for i in range(int(entry2.get())):
a = i + 1
pos_list.insert(i - 1, a)

print(player_list)
team_name = Label(edit_roster, text=entry1.get())
team_name.grid(row=1, column=3)
for n in player_list:
pr += 1
n = StringVar()
a = Entry(edit_roster, width=15, textvariable=n)
a.grid(row=pr, column=3)

# ADD_2 Append entries to entry_list #####
entry_list.append(a)

for n in number_list:
nr += 1
n = Entry(edit_roster, width=2)
n.grid(row=nr, column=1)
for n in pos_list:
posr += 1
pos = StringVar(sample)
pos.set(n + 1)
n = OptionMenu(edit_roster, pos, "G", "LW", "RW", "D")
n.grid(row=posr, column=5)
edit_roster.tkraise()


def editstats():
goal_list = []
assist_list = []
gaa_list = []
player_list = []
number_list = []
pos_list = []

pr = 3
nr = 3
gr = 3
posr = 3
ar = 3
gaar = 3

lbl5 = Label(edit_stats, text="G")
lbl5.grid(row=3, column=6)

lbl6 = Label(edit_stats, text="A")
lbl6.grid(row=3, column=5)

lbl7 = Label(edit_stats, text="GAA")
lbl7.grid(row=3, column=5)
for i in range(int(entry2.get())):
a = i + 1
goal_list.insert(i - 1, a)
for i in range(int(entry2.get())):
a = i + 1
gaa_list.insert(i - 1, a)
for i in range(int(entry2.get())):
a = "z"
for u in range(i + 1):
a += "z"
assist_list.insert(i - 1, a)
for n in goal_list:
gr += 1
n = Entry(edit_roster, width=2)
n.grid(row=gr, column=4)
for n in assist_list:
ar += 1
n = Entry(edit_roster, width=2)
n.grid(row=ar, column=2)
for n in gaa_list:
gaar += 1
n = Entry(edit_roster, width=2)
n.grid(row=gaar, column=1)
edit_roster.tkraise()


def save_new_file(s):
global player_list

# ADD_3 Get values from entries of entry_list #####
global entry_list
for i in entry_list:
print(i.get())

# fn = str(s) + ".csv"
# with open(fn, mode='w') as roster_file:
# roster_writer = csv.writer(roster_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
# roster_writer.writerow(['John Smith', 'Accounting', 'November'])
# roster_writer.writerow(['Erica Meyers', 'IT', 'March'])
# for i in player_list:
# b = i.get()
# roster_writer.writerow([b, "player 1 "])


def get_filename():
global player_list
print()
filename = simpledialog.askstring("input string", "What do you want to call your file?")
print(filename)
save_new_file(filename)


# frames
# start page
start_page = Frame(sample, bg="pink", height="50", width="60")
start_page.grid(row=0, column=0, sticky="nsew")
# new roster
new_roster = Frame(sample, bg="yellow")
new_roster.grid(row=0, column=0, sticky="nsew")
# editable roster
edit_roster = Frame(sample, bg="Blue")
edit_roster.grid(row=0, column=0, sticky="nsew")
# load roster
load_roster = Frame(sample)
load_roster.grid(row=0, column=0, sticky="nsew")
# edit stats
edit_stats = Frame(sample)
edit_stats.grid(row=0, column=0, sticky="nsew")
start_page.tkraise()

# buttons
# start page
new_roster_b = Button(start_page, command=switch_newroster, text="NEW ROSTER", height="2", width="10")
new_roster_b.grid(row=1, column=2)

load_roster_b = Button(start_page, command=switch_loadroster, text="LOAD ROSTER", height="2", width="10")
load_roster_b.grid(row=2, column=2)

# load roster
entry_getfile_b = Button(load_roster, text="Find", command=get_file)
entry_getfile_b.grid(row=2, column=3)

back_b = Button(load_roster, command=switch_startpage, text="Back")
back_b.grid(row=3, column=1)

# new roster
back_b = Button(new_roster, command=switch_startpage, text="Back")
back_b.grid(row=3, column=1)

create_b = Button(new_roster, command=create, text="Create Team")
create_b.grid(row=2, column=5)

# edit roster
save_b = Button(edit_roster, command=get_filename, text="Save")
save_b.grid(row=3, column=6)

# other widgets

# start page

# load roster
entry_lbl_getfile = Label(load_roster, text="Enter File NAME!!!")
entry_lbl_getfile.grid(row=2, column=1)

entry_getfile_act = Entry(load_roster)
entry_getfile_act.grid(row=2, column=2)

# new roster
lbl1 = Label(new_roster, text="Team Name:")
lbl1.grid(row=2, column=1)

lbl8 = Label(new_roster, text="Number of Players:")
lbl8.grid(row=2, column=3)

entry1 = Entry(new_roster)
entry1.grid(row=2, column=2)

entry2 = Entry(new_roster)
entry2.grid(row=2, column=4)

# edit roster
lbl2 = Label(edit_roster, text="#")
lbl2.grid(row=3, column=1)

lbl3 = Label(edit_roster, text="Name")
lbl3.grid(row=3, column=3)

lbl4 = Label(edit_roster, text="Pos.")
lbl4.grid(row=3, column=5)

sample.mainloop()

关于python - 有没有办法在 tkinter 中使用字符串作为输入框的变量名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54730845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com