Here is an example of a random spin button using a button surrounded by labels to create the feeling of a spin animation. This example uses the Tkinter GUI and lets you select numbers 1 to 6. If your game requires more then this, just add additional labels. You are encouraged to experiment with this code and maybe locate the labels in a more circular fashion around the button.
# create a button surrounded by labels to mimic a random spin button # tested with Python24 vegaseat 23dec2006 import Tkinter as tk # use tk namespace for Tkinter items import random def make_labels(): """ create a list of labels and position them """ labels =  for k in range(1, 7): labels.append(tk.Label(root, text=' %d ' % k, bg='yellow')) # now assign a grid position to each label for ix, label in enumerate(labels): if ix < 3: label.grid(row=0, column=ix, pady=5) elif ix == 3: button1 = tk.Button(root, text='Press me', command=click) button1.grid(row=1, column=0, columnspan=3) label.grid(row=2, column=2, pady=5) else: label.grid(row=2, column=5-ix, pady=5) return labels def click(): """ loop/spin through the labels and pick one to stop at random """ # reset all labels to same color for label in labels: label.config(bg='yellow') # start with a time delay of 15 ms and increase it in the spins t = 15 stop = random.randint(19, 24) for x in range(0, stop): label = labels[x%6] root.after(t, label.config(bg='green')) root.update() root.title(str(x%6 + 1)) # test if x == stop-1: # final result available via var1.get() var1.set(str(x%6 + 1)) break root.after(t, label.config(bg='yellow')) t += 15 # create the window form root = tk.Tk() # StringVar() updates result label automatically var1 = tk.StringVar() # set initial value var1.set("") # create the result label result = tk.Label(root, textvariable=var1, fg='red') result.grid(row=3, column=0, columnspan=3) # create the list of labels labels = make_labels() # start of program event loop root.mainloop()
Are you able to help answer this sponsored question?
Questions asked by members who have earned a lot of community kudos are featured in order to give back and encourage quality replies.
So I have a JTextfield in the cells of 1 of the columns in my JTable
I want to allow only numbers and up to one dot/period. Doesant ...
vb.Net - Regular Expression Tester
Every now and then I find another use for a regular expression. For those not familiar with regular expressions, they can be as cryptic to ...
I'm trying to build a client and a server in the same program. For example, user 1 sends a packet of data to user 2, user 2 after receiving the ...