samedi 11 novembre 2023

How can I reroll repeated random outputs?

I'm trying to make a bingo sheet randomizer for a game, but I can't figure out how to reroll my repeated options.

This is my current code:

import PySimpleGUI as sg
import random, string
items = [str(a), str(b), str(c), str(d), str(e), str(f), str(g), str(h),
         str(i), str(j), str(k), str(l), str(m), str(n), str(o), str(p),
         str(q), str(r), str(s), str(t), str(u), str(v), str(w), str(x),
         str(y), str(z), str(A), str(B), str(C), str(D), str(E), str(F),
         str(G), str(H), str(I), str(J), str(K), str(L), str(M), str(N),
         str(O), str(P), str(Q), str(R), str(S), str(T), str(U), str(V),
         str(W), str(X), str(Y), str(Z)]
# ------ Some functions to help generate data for the table ------


def word():
    return ''.join(random.choice(items) for ip in range(1))
    
def make_table(num_rows, num_cols):
    data = [[ji for ji in range(num_cols)] for ip in range(num_rows)]
    data[0] = [word() for _ in range(num_cols)]
    data[1] = [word() for _ in range(num_rows)]
    data[2] = [word() for _ in range(num_rows)]
    data[3] = [word() for _ in range(num_rows)]
    data[4] = [word() for _ in range(num_rows)]
    return data

def edit_cell(window, key, row, col, justify='left'):

    global textvariable, edit

    def callback(event, row, col, text, key):
        global edit
        widget = event.widget
        if key == 'Return':
            text = widget.get()
            print(text)
        widget.destroy()
        widget.master.destroy()
        values = list(table.item(row, 'values'))
        values[col] = text
        table.item(row, values=values)
        edit = False

    if edit or row <= 0:
        return

    edit = True
    root = window.TKroot
    table = window[key].Widget

    text = table.item(row, "values")[col]
    x, y, width, height = table.bbox(row, col)

    frame = sg.tk.Frame(root)
    frame.place(x=x, y=y, anchor="nw", width=width, height=height)
    textvariable = sg.tk.StringVar()
    textvariable.set(text)
    entry = sg.tk.Entry(frame, textvariable=textvariable, justify=justify)
    entry.pack()
    entry.select_range(0, sg.tk.END)
    entry.icursor(sg.tk.END)
    entry.focus_force()
    entry.bind("<Return>", lambda e, r=row, c=col, t=text, k='Return':callback(e, r, c, t, k))
    entry.bind("<Escape>", lambda e, r=row, c=col, t=text, k='Escape':callback(e, r, c, t, k))

def main_example1():
    global edit

    edit = False
    # ------ Make the Table Data ------
    # sg.Print('Creating table...')
    data = make_table(num_rows=5, num_cols=5)
    # headings = [str(data[0][x])+'     ..' for x in range(len(data[0]))]
    headings = ["B", "I", "N", "C", "O"]
    # sg.Print('Done creating table.  Creating GUI...')
    sg.set_options(dpi_awareness=True)
    layout = [[sg.Table(values=data, headings=headings, max_col_width=25,
                        auto_size_columns=True,
                        # display_row_numbers=True,
                        justification='right',
                        num_rows=5,
                        alternating_row_color=sg.theme_button_color()[1],
                        key='-TABLE-',
                        # selected_row_colors='red on yellow',
                        # enable_events=True,
                        # select_mode=sg.TABLE_SELECT_MODE_BROWSE,
                        expand_x=True,
                        expand_y=True,
                        enable_click_events=True,  # Comment out to not enable header and other clicks
                        )],
              [sg.Text('Cell clicked:'), sg.T(k='-CLICKED-')]]


    window = sg.Window('Table Element - Example 1', layout, resizable=True, finalize=True)

    while True:
        event, values = window.read()
        if event in (sg.WIN_CLOSED, 'Exit'):
            break
        elif isinstance(event, tuple):
            cell = row, col = event[2]
            window['-CLICKED-'].update(cell)
            edit_cell(window, '-TABLE-', row+1, col, justify='right')

    window.close()

main_example1()

All of the letters with strings have a corresponding string to go with them. It all works, but everything I've tried has either not worked or led to still repeating outputs.




Aucun commentaire:

Enregistrer un commentaire