Tkinter 函数创建无法使用 .get() 从条目添加到列表的变量

发布于 2025-01-09 04:09:12 字数 2692 浏览 1 评论 0原文

首先,我知道存在许多与 .get() 无法正常工作有关的问题,我已经阅读了其中的许多问题。 我的问题是我的程序有一个按钮,每次按下它时,它都会添加一个新的输入框以添加新的播放器,然后我想创建一个播放器对象(我在另一个文件中定义了播放器类)并将 Player 对象添加到列表中。 以下是“添加玩家”按钮的功能:

def add_player():
    new_player = Label(team_wind, text='Enter an NBA Player:')
    new_player_entry = Entry(team_wind, width=30)
    new_player.pack()
    new_player_entry.pack()
    team_wind.mainloop()
    player_list.append(new_player_entry.get())

(team_wind 是窗口,因为它正在创建玩家团队。团队类也在另一个文件中定义) 经过一番研究,我意识到主循环将终止该代码块(据我所知)。因此,当我运行该程序时,该条目返回“”。我知道这是一个非常常见的问题,但这对我来说很奇怪,因为我找不到解决方法。我已经为具有单个输入框的单个玩家提供了 tkinter 窗口的工作部分,所以我知道 .get() 是如何工作的。我尝试过使用 .update() 和 .update_idletasks()。

  • 我尝试将player_list.append移到主循环之前 因为我知道后面的代码无法访问,但是如果我将其移至 之前,然后它什么也不返回

。如果我尝试在另一个函数中或在代码终止后执行此操作,则它将不起作用,因为如果多次按下按钮,每个条目将具有相同的变量名称。我认为这意味着它必须在这个函数中完成,但不确定如何用主循环来完成。这是创建窗口以便运行的代码。我所需要的只是让它能够打印或返回包含玩家对象的列表,无论有多少玩家(取决于按下按钮的次数)。 我已经提供了运行该程序所需的代码,并删除了可能导致错误的所有其他内容。谢谢

     from tkinter import *
    player_list = []    
    def add_player():
        new_player = Label(team_wind, text='Enter an NBA Player:')
        new_player_entry = Entry(team_wind, width=30)
        new_player.pack()
        new_player_entry.pack()
        team_wind.mainloop()
        player_list.append(new_player_entry.get())
    
    def main():
        #setup
        global team_name
        global team_wind
        global player_entry
        global player_entry2
        team_wind = Tk()
        team_wind.title('Team')
        team_wind.geometry('800x500')
    
        #Text entries
        team_mode_title = Label(team_wind, text='Make a team of NBA Players and find their stats')
        player = Label(team_wind, text='Enter an NBA Player:')
        player2 = Label(team_wind, text='Enter an NBA Player:')
        
        #Text Box Entries
        player_entry = Entry(team_wind, width=30)
        player_entry2 = Entry(team_wind, width=30)
    
        #BUTTONS
        add_player_button = Button(team_wind, text='Add player', command=add_player)
    

        #Button for StackOverflow question to print the list of players
            print_list_button = Button(team_wind, text='Print List', command=print_list)
            #Pack
            team_mode_title.pack()
            #avg_button.pack()
            add_player_button.pack()
            print_list_button.pack()
            player.pack()
            player_entry.pack()
       

 player2.pack()
        player_entry2.pack()
    
        team_wind.mainloop()
    

def print_list():
    player_list.append(player_entry.get())
    player_list.append(player_entry2.get())
    print(player_list)

if __name__ == "__main__":
    main()

First off, I am aware that there are many questions out there with .get() not properly working and I have read through many of them.
My issue is that my program has a button that every time it is pressed, it adds a new entry box for a new Player to be added, then I then want to create a Player object (I have the player class defined in another file) and add the Player object to a list.
Here is the function for the "Add Player" button:

def add_player():
    new_player = Label(team_wind, text='Enter an NBA Player:')
    new_player_entry = Entry(team_wind, width=30)
    new_player.pack()
    new_player_entry.pack()
    team_wind.mainloop()
    player_list.append(new_player_entry.get())

(team_wind is the window because it is creating a Team of Players. the Team class is also defined in another file)
After some reaserch, I realized that mainloop would terminate that block of code (as far as my understanding). So, when I run the program, the entry returns ''. I know this is a very common issue, but it is wierd for mine because I can't figure out a way to get around it. I already have a working part of the tkinter window for a single player with a single entry box, so I know how .get() works. I've tried using .update() and .update_idletasks().

  • I have tried moving the player_list.append to before the mainloop
    because I know the code after is unreachable, but if I move it to
    before, then it returns nothing

. If I try to do it in another function or after the code terminates, then it won't work because if the button is pressed multiple times, each entry will have the same variable name. I think this means it has to be done in this function, but not sure how to do that with mainloop. Here is the code to create the window so it will run. All I need is for it to be able to print or return the list with the player objects for however many players there are (depends how many times the button is pressed).
I have provided the code needed to run the program and cut out everything else that would cause errors. Thanks

     from tkinter import *
    player_list = []    
    def add_player():
        new_player = Label(team_wind, text='Enter an NBA Player:')
        new_player_entry = Entry(team_wind, width=30)
        new_player.pack()
        new_player_entry.pack()
        team_wind.mainloop()
        player_list.append(new_player_entry.get())
    
    def main():
        #setup
        global team_name
        global team_wind
        global player_entry
        global player_entry2
        team_wind = Tk()
        team_wind.title('Team')
        team_wind.geometry('800x500')
    
        #Text entries
        team_mode_title = Label(team_wind, text='Make a team of NBA Players and find their stats')
        player = Label(team_wind, text='Enter an NBA Player:')
        player2 = Label(team_wind, text='Enter an NBA Player:')
        
        #Text Box Entries
        player_entry = Entry(team_wind, width=30)
        player_entry2 = Entry(team_wind, width=30)
    
        #BUTTONS
        add_player_button = Button(team_wind, text='Add player', command=add_player)
    

        #Button for StackOverflow question to print the list of players
            print_list_button = Button(team_wind, text='Print List', command=print_list)
            #Pack
            team_mode_title.pack()
            #avg_button.pack()
            add_player_button.pack()
            print_list_button.pack()
            player.pack()
            player_entry.pack()
       

 player2.pack()
        player_entry2.pack()
    
        team_wind.mainloop()
    

def print_list():
    player_list.append(player_entry.get())
    player_list.append(player_entry2.get())
    print(player_list)

if __name__ == "__main__":
    main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小苏打饼 2025-01-16 04:09:12

您应该只有一个 mainloop() - 在 main() 中。

您可以使用列表来保留小部件 Entry - 不使用 .get() - 并且在打印列表时应该使用 .get()


最少的工作代码。

我使用Frame来保留LabelEntry,这样它就在Buttons之前添加了Entry >。

import tkinter as tk  # PEP8: `import *` is not preferred

def add_player():
    label = tk.Label(frame, text='Enter an NBA Player:')
    label.pack()
    
    entry = tk.Entry(frame, width=30)
    entry.pack()
    
    entry_list.append( entry )  # add full widget, not value from widget

def print_list():
    for number, entry in enumerate(entry_list, 1):
        print( number, entry.get() )

def main():
    global team_wind
    global frame

    team_wind = tk.Tk()
    team_wind.title('Team')
    team_wind.geometry('800x500')

    team_mode_title = tk.Label(team_wind, text='Make a team of NBA Players and find their stats')
    team_mode_title.pack()

    # frame to keep all Entries
    frame = tk.Frame(team_wind)
    frame.pack()
    
    # at start add Entries for two players
    add_player()    
    add_player()
    
    # button to add Entry for next player
    add_player_button = tk.Button(team_wind, text='Add player', command=add_player)
    add_player_button.pack()

    # button to print values from all Entries
    print_list_button = tk.Button(team_wind, text='Print List', command=print_list)
    print_list_button.pack()

    team_wind.mainloop()

# --- main ---

entry_list = []

main()

You should have only one mainloop() - in main().

You could use your list to keep widgets Entry - without using .get() - and you should use .get() when you print list.


Minimal working code.

I used Frame to keep Label and Entry and this way it adds Entry before Buttons.

import tkinter as tk  # PEP8: `import *` is not preferred

def add_player():
    label = tk.Label(frame, text='Enter an NBA Player:')
    label.pack()
    
    entry = tk.Entry(frame, width=30)
    entry.pack()
    
    entry_list.append( entry )  # add full widget, not value from widget

def print_list():
    for number, entry in enumerate(entry_list, 1):
        print( number, entry.get() )

def main():
    global team_wind
    global frame

    team_wind = tk.Tk()
    team_wind.title('Team')
    team_wind.geometry('800x500')

    team_mode_title = tk.Label(team_wind, text='Make a team of NBA Players and find their stats')
    team_mode_title.pack()

    # frame to keep all Entries
    frame = tk.Frame(team_wind)
    frame.pack()
    
    # at start add Entries for two players
    add_player()    
    add_player()
    
    # button to add Entry for next player
    add_player_button = tk.Button(team_wind, text='Add player', command=add_player)
    add_player_button.pack()

    # button to print values from all Entries
    print_list_button = tk.Button(team_wind, text='Print List', command=print_list)
    print_list_button.pack()

    team_wind.mainloop()

# --- main ---

entry_list = []

main()
羁绊已千年 2025-01-16 04:09:12

一旦你的程序到达了team_wind.mainloop()命令,它下面的任何东西都不会执行,因为Python将永远继续运行mainloop()。这意味着该函数永远不会运行 player_list.append(Player(new_player_entry.get()))

Once your program reaches the team_wind.mainloop() command nothing under it will execute because python will continute running mainloop() forever. This means that the function will never get to running player_list.append(Player(new_player_entry.get())).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文