Entry
How do you make the original text in a Label change randomly using a button?
Oct 11th, 2002 07:55
Martin Franklin, chester willis,
from Tkinter import *
import random
def randomiseString(s):
numlist=[]
stringOut=[]
while (len(numlist)<len(s)):
## give me a list of numbers between 0 and len(s)
## just don't repeat and stop when i've got enough
num=random.randrange(0, len(labelText), 1)
if num not in numlist:
numlist.append(num)
for num in numlist:
stringOut.append(s[num])
stringOut="".join(stringOut)
return stringOut
root=Tk()
labelText="Random Label"
label=Label(root, text=labelText)
label.pack()
def setText():
label["text"]=randomiseString(label["text"])
b=Button(root, text="Shuffle", command=setText)
b.pack()
root.mainloop()