I'm creating a World Cup sweepstake using Python and Tkinter and I would like to have two buttons; one that selects a random person from a list of persons, and another button that selects a random world cup team from a list of teams. Each time one of each is selected it should be removed from the list so it isn't selected again. I would also like a clear button that removes the last selected player and team. The clear button seems to work but on selecting the next person and team, it sometimes picks ones that have been picked before and so isn't working.
from tkinter import *
import random
root = Tk()
root.title('World Cup 2022 - Sweepstake')
root.geometry("600x800")
def select_player():
global player_label
players = ["Matt", "Steve", "Dave", "Pete"]
chosen_player = players.pop(random.randrange(len(players)))
player_label = Label(root, text=chosen_player, font=("Arial", 16, 'bold'))
player_label.pack(pady=30)
return
def select_team():
global team_label
teams = ["Qatar", "Ecuador", "Senegal", "Netherlands"]
teams = teams.pop(random.randrange(len(teams)))
team_label = Label(root, text=teams, font=("Arial", 22, 'bold'))
team_label.pack(pady=40)
return
def clear_label():
player_label.pack_forget()
team_label.pack_forget()
top_label = Label(root, text="World Cup Sweepstake", font=("Arial", 22, 'bold'))
top_label.pack(pady=20)
select_player_button = Button(root, text="Select Player", font=("Arial", 14), command=select_player)
select_player_button.pack(pady=15)
select_team_button = Button(root, text="Select Team", font=("Arial", 14), command=select_team)
select_team_button.pack(pady=15)
delete_button = Button(root, text="Clear", command=clear_label)
delete_button.pack(pady=20)
root.mainloop()
CodePudding user response:
players and teams list should be defined outside the functions . use code below:
from tkinter import *
import random
def select_player():
global players,player_label
select_player_button.config(state="disabled")
select_team_button.config(state="active")
chosen_player = players.pop(random.randrange(len(players)))
player_label = Label(root, text=chosen_player, font=("Arial", 16, 'bold'))
player_label.pack(pady=30)
def select_team():
global teams,team_label
select_team_button.config(state="disabled")
delete_button.config(state="active")
chosen_team = teams.pop(random.randrange(len(teams)))
team_label = Label(root, text=chosen_team, font=("Arial", 22, 'bold'))
team_label.pack(pady=40)
def clear_label():
global players
delete_button.config(state="disabled")
if(len(players)>1):
select_player_button.config(state="active")
player_label.pack_forget()
team_label.pack_forget()
teams = ["Qatar", "Ecuador", "Senegal", "Netherlands"]
players = ["Matt", "Steve", "Dave", "Pete"]
root=Tk()
top_label = Label(root, text="World Cup Sweepstake", font=("Arial", 22, 'bold'))
top_label.pack(pady=20)
select_player_button = Button(root, text="Select Player", font=("Arial", 14), command=select_player)
select_player_button.pack(pady=15)
select_team_button = Button(root,state="disabled", text="Select Team", font=("Arial", 14), command=select_team)
select_team_button.pack(pady=15)
delete_button = Button(root,state="disabled", text="Clear", command=clear_label)
delete_button.pack(pady=20)
root.mainloop()
have fun :)