我无法将名称从输入添加到列表中。

huangapple go评论60阅读模式
英文:

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(&#39;game&#39;)
win.geometry(str(WIDTH) + &#39;x&#39; + str(HEIGHT))
win.attributes(&#39;-fullscreen&#39;,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 = (&#39;arial&#39;, 20), text = &#39;Choose you names&#39;, )
    name_lab.grid(row = 0, column = 1)
    
    player1 = tk.Entry(settings, font = &#39;arial&#39;)
    player1.grid(row = 3, column = 0)

    player1_lab = tk.Label(settings, font = &#39;arial&#39;, text = &#39;Imput  name of the player1:&#39;)
    player1_lab.grid(row = 2, column = 0)
    
    player2_lab = tk.Label(settings, font = &#39;arial&#39;, text = &#39;Imput  name of the player2:&#39;)
    player2_lab.grid(row = 2, column = 2)
   
    player2 = tk.Entry(settings, font = &#39;arial&#39;)
    player2.grid(row = 3, column = 2)
    
    name_save = tk.Button(settings, font = &#39;arial&#39;, text = &#39;Save&#39;, 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(&#39;game&#39;)
win.geometry(str(WIDTH) + &#39;x&#39; + str(HEIGHT))
win.attributes(&#39;-fullscreen&#39;,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 = (&#39;arial&#39;, 20), text = &#39;Choose you names&#39;, )
    name_lab.grid(row = 0, column = 1)
    
    player1 = tk.Entry(settings, font = &#39;arial&#39;)
    player1.grid(row = 3, column = 0)
    player1n = player1.get

    player1_lab = tk.Label(settings, font = &#39;arial&#39;, text = &#39;Imput  
    name of the player1:&#39;)
    player1_lab.grid(row = 2, column = 0)
    
    player2_lab = tk.Label(settings, font = &#39;arial&#39;, text = &#39;Imput  
    name of the player2:&#39;)
    player2_lab.grid(row = 2, column = 2)
   
    player2 = tk.Entry(settings, font = &#39;arial&#39;)
    player2.grid(row = 3, column = 2)
    player2n = player2.get
    
    name_save = tk.Button(settings, font = &#39;arial&#39;, text = &#39;Save&#39;, 
    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(&#39;game&#39;)
win.geometry(str(WIDTH) + &#39;x&#39; + str(HEIGHT))
win.attributes(&#39;-fullscreen&#39;,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 = (&#39;arial&#39;, 20), text = &#39;Zvol si typ hry&#39;, )
    name_lab.grid(row = 0, column = 1)
    
    sin = tk.Entry(nastavenia, font = &#39;arial&#39;, textvariable=var)
    sin.grid(row = 3, column = 0)

    sin_lab = tk.Label(nastavenia, font = &#39;arial&#39;, text = &#39;Zadaj meno 1. hraca&#39;)
    sin_lab.grid(row = 2, column = 0)
    
    mut_lab = tk.Label(nastavenia, font = &#39;arial&#39;, text = &#39;Zadaj meno 2. hraca&#39;)
    mut_lab.grid(row = 2, column = 2)
   
    mut = tk.Entry(nastavenia, font = &#39;arial&#39;, textvariable=var2)
    mut.grid(row = 3, column = 2)
    
    name_save = tk.Button(nastavenia, font = &#39;arial&#39;, text = &#39;Save&#39;, 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.

huangapple
  • 本文由 发表于 2023年5月6日 16:11:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76187829.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定