Python Tkinter列表框延迟?

Python Tkinter列表框延迟?,python,tkinter,Python,Tkinter,我试图制作一个程序,生成一个随机字符设计,让我使用python绘制,但我试图让它每两分钟给我一个设计片段。当我使用time.sleep(120)时,它会冻结并等待所有答案生成后再将它们放入列表框。有什么我可以做的不同吗 def GenerateCharTimed(): import time listbox.delete(0, END) listbox.delete(1, END) listbox.delete(2, END) listbox.delete

我试图制作一个程序,生成一个随机字符设计,让我使用python绘制,但我试图让它每两分钟给我一个设计片段。当我使用time.sleep(120)时,它会冻结并等待所有答案生成后再将它们放入列表框。有什么我可以做的不同吗

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)

这可能不完全是您想要完成的,但请尝试以下操作以避免阻塞事件循环:

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()

这可能不完全是您想要完成的,但请尝试以下操作以避免阻塞事件循环:

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()
当您使用
sleep()
整个程序(事件循环)停止时,您应该改为使用
after()
。当您使用
sleep()
整个程序(事件循环)停止时,您应该改为使用
after()