When randomizing is a thing
I am gonna show you a little app to shuffle lists. I use it to make exercises in which you have to guess the words missing in a text. One of the most boring thing about it is to write in a random order the words that are eligeble to go in the missing spaces, so I thought: “Why we do not use Python for this?”, and that is what I done. It is also a way to learn some stuff about tkinter and the entry widget throught a practical example and not just from the usual list of arguments you can put in the round brackets of the class istance to make it. At the end of this page you can find the video of the live coding too. So… let’s dive into the code.
import tkinter as tk from random import shuffle def randomize(event): sign = entry_d.get() mylist = entry.get().split(",") print(mylist) shuffle(mylist) print(mylist) mylist = f" {sign} ".join(mylist) text.delete("1.0", tk.END) text.insert(tk.END, mylist) root = tk.Tk() label = tk.Label(root, text="Separator") label.pack() v = tk.StringVar() entry_d = tk.Entry(root, font="Arial 16", textvariable=v, width=1) entry_d.pack() v.set("-") tk.Label(root, text="Enter the list you want to shuffle, separated by a comma").pack() entry = tk.Entry(root, font="Arial 16") entry.pack(fill="both") entry.focus() tk.Label(root, text="output").pack() text = tk.Text(root, font="Arial 16", width=60, height=10) text.pack() entry.bind("<Return>", randomize) entry_d.bind("<Return>", randomize) root.mainloop()
Have some live coding about shuffling stuffs