英文:
I cant add names into list from entry
问题
Here are the translated parts of your Python code:
import tkinter as tk
WIDTH = 1600
HEIGHT = 800
names = []
win = tk.Tk()
win.title('game')
win.geometry(str(WIDTH) + 'x' + str(HEIGHT))
win.attributes('-fullscreen', True)
def settings():
def save():
for widget in win.winfo_children():
widget.destroy()
names.append(player1)
names.append(player2)
print(names)
names.clear()
settings = tk.Frame(win, width=WIDTH, height=HEIGHT)
settings.pack()
# Loading names
name_lab = tk.Label(settings, font=('arial', 20), text='Choose your names')
name_lab.grid(row=0, column=1)
player1 = tk.Entry(settings, font='arial')
player1.grid(row=3, column=0)
player1_lab = tk.Label(settings, font='arial', text='Input name of player 1:')
player1_lab.grid(row=2, column=0)
player2_lab = tk.Label(settings, font='arial', text='Input name of player 2:')
player2_lab.grid(row=2, column=2)
player2 = tk.Entry(settings, font='arial')
player2.grid(row=3, column=2)
name_save = tk.Button(settings, font='arial', text='Save', command=save)
name_save.grid(row=4, column=1)
settings()
win.mainloop()
For the second code snippet, it appears to have some syntax issues. If you could provide more information about the specific problem you're encountering, I'd be happy to assist further.
英文:
Im trying to make quiz game in python with tk. I got stuck at adding and taking in names of the players, but when I write names into entry it prints this instead of the names: [<tkinter.Entry object .!frame.!entry>, <tkinter.Entry object .!frame.!entry2>].
import tkinter as tk
WIDTH = 1600
HEIGHT = 800
names = []
win = tk.Tk()
win.title('game')
win.geometry(str(WIDTH) + 'x' + str(HEIGHT))
win.attributes('-fullscreen',True)
def settings():
def save():
for widget in win.winfo_children():
widget.destroy()
names.append(player1)
names.append(player2)
print(names)
names.clear()
settings = tk.Frame(win, width = WIDTH, height = HEIGHT)
settings.pack()
#nacitanie mien
name_lab = tk.Label(settings, font = ('arial', 20), text = 'Choose you names', )
name_lab.grid(row = 0, column = 1)
player1 = tk.Entry(settings, font = 'arial')
player1.grid(row = 3, column = 0)
player1_lab = tk.Label(settings, font = 'arial', text = 'Imput name of the player1:')
player1_lab.grid(row = 2, column = 0)
player2_lab = tk.Label(settings, font = 'arial', text = 'Imput name of the player2:')
player2_lab.grid(row = 2, column = 2)
player2 = tk.Entry(settings, font = 'arial')
player2.grid(row = 3, column = 2)
name_save = tk.Button(settings, font = 'arial', text = 'Save', command = save)
name_save.grid(row = 4, column = 1)
settings()
win.mainloop()
I also tried using function get(), but that doesnt work either, it just gives me empty quotation marks instead of the names.
import tkinter as tk
WIDTH = 1600
HEIGHT = 800
names = []
win = tk.Tk()
win.title('game')
win.geometry(str(WIDTH) + 'x' + str(HEIGHT))
win.attributes('-fullscreen',True)
def settings():
def save():
for widget in win.winfo_children():
widget.destroy()
names.append(player1n)
names.append(player2n)
print(names)
names.clear()
settings = tk.Frame(win, width = WIDTH, height = HEIGHT)
settings.pack()
#nacitanie mien
name_lab = tk.Label(settings, font = ('arial', 20), text = 'Choose you names', )
name_lab.grid(row = 0, column = 1)
player1 = tk.Entry(settings, font = 'arial')
player1.grid(row = 3, column = 0)
player1n = player1.get
player1_lab = tk.Label(settings, font = 'arial', text = 'Imput
name of the player1:')
player1_lab.grid(row = 2, column = 0)
player2_lab = tk.Label(settings, font = 'arial', text = 'Imput
name of the player2:')
player2_lab.grid(row = 2, column = 2)
player2 = tk.Entry(settings, font = 'arial')
player2.grid(row = 3, column = 2)
player2n = player2.get
name_save = tk.Button(settings, font = 'arial', text = 'Save',
command = save)
name_save.grid(row = 4, column = 1)
settings()
win.mainloop()
答案1
得分: 1
声明两个StringVar
来保存Entry
输入内容。
import tkinter as tk
WIDTH = 1600
HEIGHT = 800
names = []
win = tk.Tk()
win.title('game')
win.geometry(str(WIDTH) + 'x' + str(HEIGHT))
win.attributes('-fullscreen', True)
def nastavenia():
def save():
for widget in win.winfo_children():
widget.destroy()
# 将输入追加到列表中。
names.append(var.get())
names.append(var2.get())
var = tk.StringVar() # 字符串变量 1
var2 = tk.StringVar() # 字符串变量 2
nastavenia = tk.Frame(win, width=WIDTH, height=HEIGHT)
nastavenia.pack()
name_lab = tk.Label(nastavenia, font=('arial', 20), text='选择游戏类型', )
name_lab.grid(row=0, column=1)
sin = tk.Entry(nastavenia, font='arial', textvariable=var)
sin.grid(row=3, column=0)
sin_lab = tk.Label(nastavenia, font='arial', text='输入玩家1的名字')
sin_lab.grid(row=2, column=0)
mut_lab = tk.Label(nastavenia, font='arial', text='输入玩家2的名字')
mut_lab.grid(row=2, column=2)
mut = tk.Entry(nastavenia, font='arial', textvariable=var2)
mut.grid(row=3, column=2)
name_save = tk.Button(nastavenia, font='arial', text='保存', command=save)
name_save.grid(row=4, column=1)
nastavenia()
win.mainloop()
现在,names
列表将被填充上您的输入。编辑:您已经有一个名为names
的变量。
英文:
Declare two StringVar
to hold the contents of the inputs to the Entry
's.
import tkinter as tk
WIDTH = 1600
HEIGHT = 800
names = []
win = tk.Tk()
win.title('game')
win.geometry(str(WIDTH) + 'x' + str(HEIGHT))
win.attributes('-fullscreen',True)
def nastavenia():
def save():
for widget in win.winfo_children():
widget.destroy()
# Append the inputs to the list.
names.append(var.get())
names.append(var2.get())
# Deleted names.clear()
var = tk.StringVar() # String variable no. 1
var2=tk.StringVar() # String variable no. 2
nastavenia = tk.Frame(win, width = WIDTH, height = HEIGHT)
nastavenia.pack()
#nacitanie mien
name_lab = tk.Label(nastavenia, font = ('arial', 20), text = 'Zvol si typ hry', )
name_lab.grid(row = 0, column = 1)
sin = tk.Entry(nastavenia, font = 'arial', textvariable=var)
sin.grid(row = 3, column = 0)
sin_lab = tk.Label(nastavenia, font = 'arial', text = 'Zadaj meno 1. hraca')
sin_lab.grid(row = 2, column = 0)
mut_lab = tk.Label(nastavenia, font = 'arial', text = 'Zadaj meno 2. hraca')
mut_lab.grid(row = 2, column = 2)
mut = tk.Entry(nastavenia, font = 'arial', textvariable=var2)
mut.grid(row = 3, column = 2)
name_save = tk.Button(nastavenia, font = 'arial', text = 'Save', command = save)
name_save.grid(row = 4, column = 1)
nastavenia()
win.mainloop()
Now the names
gets filled with your input.
Edit: You had already had a variable named names
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论