I am trying to make a program that generates a random character design for me to draw using python, but I am trying to make it give me a snippet of the design every two minutes. When I use time.sleep(120), it freezes up and waits until all the answers are generated before it puts them into the list box. Is there anything I can do differently?
def GenerateCharTimed():
import time
listbox.delete(0, END)
listbox.delete(1, END)
listbox.delete(2, END)
listbox.delete(3, END)
listbox.delete(4, END)
listbox.delete(5, END)
characterDrawn = random.choice(beings)
haircolor = random.choice(colors)
charhairstyle = random.choice(hairstyle)
features = random.choice(faceFeatures)
notperson = True
while notperson == True:
characterDrawn = random.choice(beings)
if characterDrawn == "man" or characterDrawn == "boy" or characterDrawn == "girl" or characterDrawn == "woman" or characterDrawn == "ghost" or characterDrawn == "vampire" or characterDrawn == "gnome" or characterDrawn == "skeleton" or characterDrawn == "gnome" or characterDrawn == "zombie" or characterDrawn == "plant monster" or characterDrawn == "humanoid" or characterDrawn == "monster":
notperson = False
if charhairstyle == "no hair":
character = "Your character is a " + characterDrawn + " with a " + features + " face, and with" + " " + charhairstyle + "."
character = str(character)
listbox.insert(END, character)
else:
character = "Your character is a " + characterDrawn + " with a " + features + " face, and with a " + haircolor + " " + charhairstyle
character = str(character)
listbox.insert(END, character)
time.sleep(120)
character = "Your character is wearing " + random.choice(clothing) + "."
character = str(character)
listbox.insert(END, character)
time.sleep(120)
adjLook1 = random.choice(adjectiveLook)
adjLook2 = random.choice(adjectiveLook)
adjLook3 = random.choice(adjectiveLook)
limb1 = random.choice(limbs)
limb2 = random.choice(limbs)
adjLookSimilar = False
if adjLook1 == adjLook2 or adjLook2 == adjLook3 or adjLook3 == adjLook1 or limb1 == limb2:
adjLookSimilar = True
while adjLookSimilar == True:
adjLook1 = random.choice(adjectiveLook)
adjLook2 = random.choice(adjectiveLook)
adjLook3 = random.choice(adjectiveLook)
limb1 = random.choice(limbs)
limb2 = random.choice(limbs)
if adjLook1 != adjLook2 and adjLook2 != adjLook3 and adjLook3 != adjLook1:
adjLookSimilar = False
character = "Your character has " + adjLook1 + " " + limb1 + "."
character = str(character)
listbox.insert(END, character)
time.sleep(120)
character = "Your character has " + adjLook2 + " " + limb2 + "."
character = str(character)
listbox.insert(END, character)
This may not be entirely what you're trying to accomplish, but try something like this to avoid blocking the event loop:
from tkinter import *
import time
import random
class Character(Listbox):
def __init__(self, parent):
Listbox.__init__(self, parent)
self.pack(expand=True, fill='x')
self.beings = ['alien', 'boy', 'girl', 'truck']
self.colors = ['white', 'red', 'brown', 'purple', 'black', 'gray']
self.hairstyle = ['fro', 'mullet', 'modern', 'wig', 'natural', 'weave', 'bowl']
self.faceFeatures = ['eyes', 'nose', 'mouth', 'ears', 'freckles']
self.clothing = ['shirt', 'pants', 'socks', 'beanie', 'scarf']
self.adjectiveLook = ['colorful', 'exotic', 'transparent', 'ugly', 'beautiful']
self.limbs = ['leg', 'arm', 'finger', 'toe']
def GenerateCharTimed(self):
self.delete(0, END)
characterDrawn = random.choice(self.beings)
haircolor = random.choice(self.colors)
charhairstyle = random.choice(self.hairstyle)
features = random.choice(self.faceFeatures)
notperson = True
while notperson == True:
characterDrawn = random.choice(self.beings)
if characterDrawn == "man" or characterDrawn == "boy" or characterDrawn == "girl" or characterDrawn == "woman" or characterDrawn == "ghost" or characterDrawn == "vampire" or characterDrawn == "gnome" or characterDrawn == "skeleton" or characterDrawn == "gnome" or characterDrawn == "zombie" or characterDrawn == "plant monster" or characterDrawn == "humanoid" or characterDrawn == "monster":
notperson = False
if charhairstyle == "no hair":
character = "Your character is a " + characterDrawn + " with a " + features + " face, and with" + " " + charhairstyle + "."
character = str(character)
self.insert(END, character)
else:
character = "Your character is a " + characterDrawn + " with a " + features + " face, and with a " + haircolor + " " + charhairstyle
character = str(character)
self.insert(END, character)
# time.sleep(120)
character = "Your character is wearing " + random.choice(self.clothing) + "."
character = str(character)
self.insert(END, character)
# time.sleep(120)
adjLook1 = random.choice(self.adjectiveLook)
adjLook2 = random.choice(self.adjectiveLook)
adjLook3 = random.choice(self.adjectiveLook)
limb1 = random.choice(self.limbs)
limb2 = random.choice(self.limbs)
adjLookSimilar = False
if adjLook1 == adjLook2 or adjLook2 == adjLook3 or adjLook3 == adjLook1 or limb1 == limb2:
adjLookSimilar = True
while adjLookSimilar == True:
adjLook1 = random.choice(self.adjectiveLook)
adjLook2 = random.choice(self.adjectiveLook)
adjLook3 = random.choice(self.adjectiveLook)
limb1 = random.choice(self.limbs)
limb2 = random.choice(self.limbs)
if adjLook1 != adjLook2 and adjLook2 != adjLook3 and adjLook3 != adjLook1:
adjLookSimilar = False
character = "Your character has " + adjLook1 + " " + limb1 + "."
character = str(character)
self.insert(END, character)
# time.sleep(120)
character = "Your character has " + adjLook2 + " " + limb2 + "."
character = str(character)
self.insert(END, character)
# After 5 seconds, run this function again
self.after(5000, self.GenerateCharTimed)
root = Tk()
character = Character(root)
character.GenerateCharTimed()
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.