English 中文(简体)
如何在python GUI tkinter中从列表中删除单词
原标题:How to delete word from list in python GUI tkinter

试图生成一个随机团队,然后将其从选择列表中删除,希望添加“没有团队”的显示,但需要一些帮助

from tkinter import *
import random


def teams():
    words_list = [ Atlanta Hawks ,
                   Boston Celtics ,
                   Brooklyn Nets ,
                   Charlotte Hornets ,
                   Chicago Bulls ,
                   Cleveland Cavaliers ,
                   Dallas Mavericks ,
                    Denver Nuggets ,
                    Detroit Pistons ,
                    Golden State Warriors ,
                     Houston Rockets ,
                    Indiana Pacers ,
                   Los Angeles Clippers ,
                   Los Angeles Lakers ,
                   Memphis Grizzlies ,
                   Miami Heat ,
                   Milwaukee Bucks ,
                   Minnesota Timberwolves ,
                   New Orleans Pelicans ,
                   New York Knicks ,
                   Oklahoma City Thunder ,
                   Orlando Magic ,
                   Philadelphia 76ers ,
                   Phoenix Suns ,
                    Portland Trail Blazers ,
                   Sacramento Kings ,
                   San Antonio Spurs ,
                   Toronto Raptors ,
                   Utah Jazz ,
                   Washington Wizards ]

    while words_list:
        team = words_list.pop(random.randrange(len(words_list)))
        label.config(text=team)


window = Tk()
icon = PhotoImage(file= IMG_0612.png )
window.iconphoto(True, icon)
button = Button(window, text= Click me!!! )
button.config(command=teams)  # performs call back of function
button.config(font=( Ink Free , 20,  bold ))
button.config(bg= #ff6200 )
button.config(fg= #fffb1f )
button.config(activebackground= #FF0000 )
button.config(activeforeground= #fffb1f )
image = PhotoImage(file= IMG_0612.png )
button.config(image=image)
button.config(compound= bottom )
label = Label(window)
label.config(font=( Monospace , 50))
label.pack()
button.pack()
window.mainloop()

当所有单词都被删除时,我正在排除崩溃,但列表只是随着按钮重复,表现得就像删除代码根本不存在一样。

问题回答

您需要将数据移出函数,这样就不会在每次调用时都重新创建它。您还需要一个列表来复制到,这样您就不会丢失整个数据库。

import tkinter as tk #importing an entire package with * is bad practice
import random

#teams database
teams_db = [ Atlanta Hawks ,
             Boston Celtics ,
             Brooklyn Nets ,
             Charlotte Hornets ,
             Chicago Bulls ,
             Cleveland Cavaliers ,
             Dallas Mavericks ,
             Denver Nuggets ,
             Detroit Pistons ,
             Golden State Warriors ,
             Houston Rockets ,
             Indiana Pacers ,
             Los Angeles Clippers ,
             Los Angeles Lakers ,
             Memphis Grizzlies ,
             Miami Heat ,
             Milwaukee Bucks ,
             Minnesota Timberwolves ,
             New Orleans Pelicans ,
             New York Knicks ,
             Oklahoma City Thunder ,
             Orlando Magic ,
             Philadelphia 76ers ,
             Phoenix Suns ,
             Portland Trail Blazers ,
             Sacramento Kings ,
             San Antonio Spurs ,
             Toronto Raptors ,
             Utah Jazz ,
             Washington Wizards ]
            
#list for processing     
teams_list = None
         
def teams():
    global teams_list
    
    #reset teams_list to a fresh copy of the database if it is empty
    if not teams_list: 
        teams_list = teams_db[:]
    #set label
    label.config(text=teams_list.pop(random.randrange(len(teams_list))))


window = tk.Tk()
icon = tk.PhotoImage(file= IMG_0612.png )
window.iconphoto(True, icon)
button = tk.Button(window, text= Click me!!! )
button.config(command=teams)  # performs call back of function
button.config(font=( Ink Free , 20,  bold ))
button.config(bg= #ff6200 )
button.config(fg= #fffb1f )
button.config(activebackground= #FF0000 )
button.config(activeforeground= #fffb1f )
#you already have a reference to this image
image = tk.PhotoImage(file= IMG_0612.png )
button.config(image=image)
button.config(compound= bottom )
label = tk.Label(window)
label.config(font=( Monospace , 50))
label.pack()
button.pack()
window.mainloop()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签