I wrote this code to allow users to choose the number of players in a tournament. The options are all powers of 2 so it can eventually end with a 1vs1. I want the next screen to allow users to input the names of each player. For this, I need the code to take the option selected by the user and create that many entry widgets in which the user can input the names of each player. For example, if the user selected the option "4", then the next screen would contain 4 entry widgets in which the user can input the names of each of the 4 players. How would I write this code?
from logging import root
from tkinter import *
import os
from turtle import bgcolor, screensize
import tkinter as tk
def enter_numberofitems1():
enter_numberofitems1_screen = Tk()
enter_numberofitems1_screen.geometry("1000x500")
enter_numberofitems1_screen.title("Enter Number of Players/Teams")
Label(enter_numberofitems1_screen, text= "Choose Number of Players/Teams", bg="yellow",
width="300", height="5", font=("Calibri", 20)).pack()
Var = IntVar()
Radiobutton(enter_numberofitems1_screen, text="4", variable=Var, value=4, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="8", variable=Var, value=8, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="16", variable=Var, value=16, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="32", variable=Var, value=32, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="64", variable=Var, value=64, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Button(enter_numberofitems1_screen, text="Submit", height="5", width = "30",
bg="green").pack()
CodePudding user response:
You have a lot of mistakes in your code above:
1- You imported a lot of libraries which have no use in your code
2- You put your code inside a function, when you run your code nothing will happen as you didn't call the function
You only need to import
from tkinter import *
And delete the function called enter_numberofitems1
Full Code:
from tkinter import *
def radio_input():
teams_gui = Tk()
teams_gui.title("Teams")
teams_gui.geometry("500x750")
no_teams = Var.get()
if no_teams == 4:
for i in range(4):
entry = Entry(teams_gui)
entry.pack()
elif no_teams == 8:
for i in range(8):
entry = Entry(teams_gui)
entry.pack()
elif no_teams == 16:
for i in range(16):
entry = Entry(teams_gui)
entry.pack()
elif no_teams == 32:
for i in range(32):
entry = Entry(teams_gui)
entry.pack()
elif no_teams == 64:
for i in range(64):
entry = Entry(teams_gui)
entry.pack()
enter_numberofitems1_screen = Tk()
enter_numberofitems1_screen.geometry("1000x500")
enter_numberofitems1_screen.title("Enter Number of Players/Teams")
Label(enter_numberofitems1_screen, text= "Choose Number of Players/Teams", bg="yellow",
width="300", height="5", font=("Calibri", 20)).pack()
Var = IntVar()
Radiobutton(enter_numberofitems1_screen, text="4", variable=Var, value=4, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="8", variable=Var, value=8, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="16", variable=Var, value=16, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="32", variable=Var, value=32, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Radiobutton(enter_numberofitems1_screen, text="64", variable=Var, value=64, height="2",
width= "10", fg="green", font=("Calibri", 10)).pack()
Button(enter_numberofitems1_screen, text="Submit", height="5", width = "30",
bg="green", command=radio_input).pack()
enter_numberofitems1_screen.mainloop()
CodePudding user response:
Button should have command=function_name
to run function which will get value and create new widgets.
And you have to use global
variable to keep value and get it in another
function. Without global
it will keep it in local variable and you can't access it from other function
If you want to show new elements in the same window then you could keep widgets in Frame
and later you can destroy()
this frame to remove all widgets. OR use .pack_forget()
to only hide frame - and later you can use again .pack()
to show the same frame with previously selected value.
And if you want to show in second window (and keep previous one) then you should use Toplevel
instead of Tk
to create next windows.
Minimal working code.
I removed fonts and colors to make it more readable.
I used for
-loop to create all radiobuttons.
import tkinter as tk # PEP8: `import *` is not preferred
# --- functions --- # PEP8: all functions before main code
def get_names():
global entries
for e in entries:
print('name:', e.get())
def get_value():
global frame
global entries
print('selected:', var.get())
if var.get() > 0:
frame.destroy()
frame = tk.Frame(enter_numberofitems1_screen)
frame.pack() # has to be in separated line to assing `Frame()` to `frame`, not `pack()` to `frame`
tk.Label(frame, text=f'Selected: {var.get()}').pack()
entries = []
for x in range(var.get()):
e = tk.Entry(frame)
e.pack()
entries.append(e)
tk.Button(frame, text="Submit", command=get_names).pack()
def enter_numberofitems1():
global var # inform function that `var = ...` will have to assign to global variable `var` instead of local variable `var`
global frame
global enter_numberofitems1_screen
enter_numberofitems1_screen = tk.Tk()
enter_numberofitems1_screen.geometry("1000x500")
enter_numberofitems1_screen.title("Enter Number of Players/Teams")
frame = tk.Frame(enter_numberofitems1_screen)
frame.pack() # has to be in separated line to assing `Frame()` to `frame`, not `pack()` to `frame`
tk.Label(frame, text="Choose Number of Players/Teams").pack()
var = tk.IntVar(enter_numberofitems1_screen)
for x in range(2, 7):
value = 2**x
tk.Radiobutton(frame, text=str(value), variable=var, value=value).pack()
tk.Button(frame, text="Submit", command=get_value).pack()
enter_numberofitems1_screen.mainloop()
# --- main ---
enter_numberofitems1()