Home > database >  How do I create a table with names inputted by the user?
How do I create a table with names inputted by the user?

Time:09-05

I created this piece of code so a user can enter the number of teams participating in a sports tournament. The code then opens up that many number of entry widgets for the user to input the names of each team.

from tkinter import * 

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 = Frame(enter_numberofitems1_screen)
            frame.pack()  
            
            Label(frame, text=f'Selected: {var.get()}', font=("Calibri", 15)).pack()
            Label(frame, text="").pack()
            Label(frame, text="Enter Names", font=("Calibri", 15)).pack()
            Label(frame, text="").pack()
            
            entries = []
            for x in range(var.get()):
                e = Entry(frame)
                e.pack()
                entries.append(e)
    
            Label(frame, text="").pack()
            Button(frame, text="Submit", bg="green", height="5", width="30", command=get_names).pack()
            
    #Designing window for selecting number of players/teams(max 64)
    def enter_numberofitems1():
        global var  
        global frame
        global enter_numberofitems1_screen
    
        enter_numberofitems1_screen = Tk()
        enter_numberofitems1_screen.geometry("1000x500")
        enter_numberofitems1_screen.title("Enter Number of Players/Teams")
       
        frame = Frame(enter_numberofitems1_screen)
        frame.pack()  
        
        Label(frame, text="Choose Number of Players/Teams", bg="yellow", width="300", height="5", font=("Calibri", 20)).pack()
        var = IntVar(enter_numberofitems1_screen)
    
        for x in range(2, 7):
            value = 2**x
            Radiobutton(frame, text=str(value), variable=var, value=value, fg="green", height="1", width="10").pack()
        
        Button(frame, text="Submit", command=get_value, height="5", width="30", bg="green").pack()

enter_numberofitems1_screen.mainloop

For the next screen, I want there to be a table to show the matches being played between the inputted teams. Something like the image I have shared below. Each of the letters would instead be the names of the teams inputted by the user into the entry widgets.

Example

How do I achieve this, since there is no option to create a table in tkinter? And how would I collect the names inputted by the user and randomize them and display them on the table? Please help.

CodePudding user response:

Assuming you have your matches laid out in a list of 2-tuples, you can just

import tkinter as tk

def show_matches(matches):
    dialog = tk.Tk()
    tk.Label(dialog, text="Team 1").grid(row=0, column=1)
    tk.Label(dialog, text="Team 2").grid(row=0, column=2)
    for i, (team_1, team_2) in enumerate(matches, 1):
        tk.Label(dialog, text=f"Match {i}").grid(row=i, column=0)
        tk.Label(dialog, text=team_1).grid(row=i, column=1)
        tk.Label(dialog, text=team_2).grid(row=i, column=2)
    dialog.mainloop()


show_matches([
    ("Spam", "Eggs"),
    ("Pea Soup", "Booze"),
    ("John", "Scaffold"),
    ("Foo", "Bar"),
])

to get something like

enter image description here

CodePudding user response:

It's John from Codemy, Noticed that most of the codes that you have used are from my videos. Please email me at [email protected]. I can help you if you would like

  • Related