time.sleep让tkinter崩溃还有什么我可以用的吗



所以我正在根据CS:GO的赔率制作一个案例揭幕战,并试图使用随机数生成制作某种动画。

示例:

while x <10:
openr = random.randint (0,10000)
#blue
if openr < 7992:
anim_txt_5 = anim_txt_4
anim_txt_4 = anim_txt_3
anim_txt_3 = anim_txt_2
anim_txt_2 = anim_txt_1
anim_txt_1 = " Mil Spec "
anim_col_5 = anim_col_4
anim_col_4 = anim_col_3
anim_col_3 = anim_col_2
anim_col_2 = anim_col_1
anim_col_1 = 'blue'
x+= 1
z*= 2
anim_1.config(text = anim_txt_5, bg = anim_col_5)
anim_2.config(text = anim_txt_4, bg = anim_col_4)
anim_3.config(text = anim_txt_3, bg = anim_col_3)
anim_4.config(text = anim_txt_2, bg = anim_col_2)
anim_5.config(text = anim_txt_1, bg = anim_col_1)

但是,如果我添加time.sleep()使动画看起来像是滚动的tkinter,那么它就会崩溃。还有什么可以让它等待的吗。x只是说它要做多少次动画,z是时间,但我还没有设置正确的乘法来使它正确。

所有代码

import tkinter as tk
import random
import time
root = tk.Tk()
root.title ('not stonks simulator')
root.resizable (480, 480)
#variabales for results
list_col_1 = 'light grey'
list_col_2 = 'light grey'
list_col_3 = 'light grey'
list_col_4 = 'light grey'
list_col_5 = 'light grey'
list_1 = " "
list_2 = " "
list_3 = " "
list_4 = " "
list_5 = " "
#variables for opening animation
x = 0
z = .01
anim_col_1 = 'grey'
anim_col_2 = 'grey'
anim_col_3 = 'grey'
anim_col_4 = 'grey'
anim_col_5 = 'grey'
anim_txt_1 = "       "
anim_txt_2 = "       "
anim_txt_3 = "       "
anim_txt_4 = "       "
anim_txt_5 = "       "
def opener():
global list_1, list_2, list_3, list_4, list_5, list_col_1, list_col_2, list_col_3, list_col_4, list_col_5, x, z, anim_col_1, anim_col_2, anim_col_3, anim_col_4, anim_col_5, anim_txt_1, anim_txt_2, anim_txt_3,anim_txt_4, anim_txt_5
num = random.randint(0,10000)
x = 0
#opening animation thing
while x <10:
openr = random.randint (0,10000)
#blue
if openr < 7992:
anim_txt_5 = anim_txt_4
anim_txt_4 = anim_txt_3
anim_txt_3 = anim_txt_2
anim_txt_2 = anim_txt_1
anim_txt_1 = " Mil Spec "
anim_col_5 = anim_col_4
anim_col_4 = anim_col_3
anim_col_3 = anim_col_2
anim_col_2 = anim_col_1
anim_col_1 = 'blue'
x+= 1
z*= 2
anim_1.config(text = anim_txt_5, bg = anim_col_5)
anim_2.config(text = anim_txt_4, bg = anim_col_4)
anim_3.config(text = anim_txt_3, bg = anim_col_3)
anim_4.config(text = anim_txt_2, bg = anim_col_2)
anim_5.config(text = anim_txt_1, bg = anim_col_1)
#purple
if openr >= 9590 and num <= 9909:
anim_txt_5 = anim_txt_4
anim_txt_4 = anim_txt_3
anim_txt_3 = anim_txt_2
anim_txt_2 = anim_txt_1
anim_txt_1 = "Restricted"
anim_col_5 = anim_col_4
anim_col_4 = anim_col_3
anim_col_3 = anim_col_2
anim_col_2 = anim_col_1
anim_col_1 = 'purple'
x += 1
z *= 2
anim_1.config(text=anim_txt_5, bg=anim_col_5)
anim_2.config(text=anim_txt_4, bg=anim_col_4)
anim_3.config(text=anim_txt_3, bg=anim_col_3)
anim_4.config(text=anim_txt_2, bg=anim_col_2)
anim_5.config(text=anim_txt_1, bg=anim_col_1)
#pink
if openr >= 9909 and num <= 9973:
anim_txt_5 = anim_txt_4
anim_txt_4 = anim_txt_3
anim_txt_3 = anim_txt_2
anim_txt_2 = anim_txt_1
anim_txt_1 = "Classified"
anim_col_5 = anim_col_4
anim_col_4 = anim_col_3
anim_col_3 = anim_col_2
anim_col_2 = anim_col_1
anim_col_1 = 'pink'
x += 1
z *= 2
anim_1.config(text=anim_txt_5, bg=anim_col_5)
anim_2.config(text=anim_txt_4, bg=anim_col_4)
anim_3.config(text=anim_txt_3, bg=anim_col_3)
anim_4.config(text=anim_txt_2, bg=anim_col_2)
anim_5.config(text=anim_txt_1, bg=anim_col_1)
#red
if openr >= 9909 and num <= 9973:
anim_txt_5 = anim_txt_4
anim_txt_4 = anim_txt_3
anim_txt_3 = anim_txt_2
anim_txt_2 = anim_txt_1
anim_txt_1 = "  Covert  "
anim_col_5 = anim_col_4
anim_col_4 = anim_col_3
anim_col_3 = anim_col_2
anim_col_2 = anim_col_1
anim_col_1 = 'red'
x += 1
z *= 2
anim_1.config(text=anim_txt_5, bg=anim_col_5)
anim_2.config(text=anim_txt_4, bg=anim_col_4)
anim_3.config(text=anim_txt_3, bg=anim_col_3)
anim_4.config(text=anim_txt_2, bg=anim_col_2)
anim_5.config(text=anim_txt_1, bg=anim_col_1)
#yellow
if openr >= 9973 and num <= 10000:
anim_txt_5 = anim_txt_4
anim_txt_4 = anim_txt_3
anim_txt_3 = anim_txt_2
anim_txt_2 = anim_txt_1
anim_txt_1 = "  Knife   "
anim_col_5 = anim_col_4
anim_col_4 = anim_col_3
anim_col_3 = anim_col_2
anim_col_2 = anim_col_1
anim_col_1 = 'yellow'
x += 1
z *= 2
anim_1.config(text=anim_txt_5, bg=anim_col_5)
anim_2.config(text=anim_txt_4, bg=anim_col_4)
anim_3.config(text=anim_txt_3, bg=anim_col_3)
anim_4.config(text=anim_txt_2, bg=anim_col_2)
anim_5.config(text=anim_txt_1, bg=anim_col_1)
if x > 100:
#blue
if num < 7992:
label_3.config(text="Mil Spec", bg='blue')
#names for list
list_5 = list_4
list_4 = list_3
list_3 = list_2
list_2 = list_1
list_1 = "Mil Spec"
#colors for list
list_col_5 = list_col_4
list_col_4 = list_col_3
list_col_3 = list_col_2
list_col_2 = list_col_1
list_col_1 = 'blue'
#updating labels
label_4.config(text=list_1, bg=list_col_1)
label_5.config(text=list_2, bg=list_col_2)
label_6.config(text=list_3, bg=list_col_3)
label_7.config(text=list_4, bg=list_col_4)
label_8.config(text=list_5, bg=list_col_5)
#purple
if num >= 7992 and num <= 9590:
label_3.config(text="Restricted", bg='purple')
#names for list
list_5 = list_4
list_4 = list_3
list_3 = list_2
list_2 = list_1
list_1 = "Restricted"
#list for colors
lsit_col_5 = list_col_4
lsit_col_4 = list_col_3
lsit_col_3 = list_col_2
lsit_col_2 = list_col_1
list_col_1 = 'purple'
#updating labels
label_4.config(text=list_1, bg=list_col_1)
label_5.config(text=list_2, bg=list_col_2)
label_6.config(text=list_3, bg=list_col_3)
label_7.config(text=list_4, bg=list_col_4)
label_8.config(text=list_5, bg=list_col_5)
#pink
if num >= 9590 and num <= 9909:
label_3.config(text="Classified", bg= 'pink')
#names for list
list_5 = list_4
list_4 = list_3
list_3 = list_2
list_2 = list_1
#colors for list
list_1 = "Classified"
lsit_col_5 = list_col_4
lsit_col_4 = list_col_3
lsit_col_3 = list_col_2
lsit_col_2 = list_col_1
list_col_1 = 'pink'
# updating labels
label_4.config(text=list_1, bg=list_col_1)
label_5.config(text=list_2, bg=list_col_2)
label_6.config(text=list_3, bg=list_col_3)
label_7.config(text=list_4, bg=list_col_4)
label_8.config(text=list_5, bg=list_col_5)
#red
if num >= 9909 and num <= 9973:
label_3.config(text="Covert", bg='red')
#names for list
list_5 = list_4
list_4 = list_3
list_3 = list_2
list_2 = list_1
list_1 = "Covert"
#colors for list
lsit_col_5 = list_col_4
lsit_col_4 = list_col_3
lsit_col_3 = list_col_2
lsit_col_2 = list_col_1
list_col_1 = 'red'
# updating labels
label_4.config(text=list_1, bg=list_col_1)
label_5.config(text=list_2, bg=list_col_2)
label_6.config(text=list_3, bg=list_col_3)
label_7.config(text=list_4, bg=list_col_4)
label_8.config(text=list_5, bg=list_col_5)
#knife
if num >= 9973 and num <= 10000:
label_3.config(text="Exceedingly Rare", bg='yellow')
#names for list
list_5 = list_4
list_4 = list_3
list_3 = list_2
list_2 = list_1
list_1 = "Exceedingly Rare"
#colors for list
lsit_col_5 = list_col_4
lsit_col_4 = list_col_3
lsit_col_3 = list_col_2
lsit_col_2 = list_col_1
list_col_1 = 'yellow'
# updating labels
label_4.config(text=list_1, bg=list_col_1)
label_5.config(text=list_2, bg=list_col_2)
label_6.config(text=list_3, bg=list_col_3)
label_7.config(text=list_4, bg=list_col_4)
label_8.config(text=list_5, bg=list_col_5)
else:
pass
else:
pass

#labels
label_1 = tk.Label(root,  text = "Case Opener Made by Storm")
label_2 = tk.Label(root, text = "You opened: ")
label_3 = tk.Label(root, text = "       ")
label_4 = tk.Label(root)
label_5 = tk.Label(root)
label_6 = tk.Label(root)
label_7 = tk.Label(root)
label_8 = tk.Label(root)
label_9 = tk.Label(root, text = "Last opened:")
#labels for opening animation
anim_1 = tk.Label (root)
anim_2 = tk.Label (root)
anim_3 = tk.Label (root)
anim_4 = tk.Label (root)
anim_5 = tk.Label (root)
#button
Button_1 =tk.Button (text =  "click to open",command = opener)
#placements for main elements
#label_1.grid (row = 0, column = 0)
label_2.grid (row = 2, column = 0)
label_3.grid (row = 2, column = 1)
#placements for list of recieved items
label_4.grid (row = 2, column = 7)
label_5.grid (row = 3, column = 7)
label_6.grid (row = 4, column = 7)
label_7.grid (row = 5, column = 7)
label_8.grid (row = 6, column = 7)
label_9.grid (row = 0, column = 7)
#placements for animation labels
anim_1.grid (row =1, column =1)
anim_2.grid (row =1, column =2)
anim_3.grid (row =1, column =3)
anim_4.grid (row =1, column =4)
anim_5.grid (row =1, column =5)

#placement for opener button
Button_1.grid (row = 2, column = 0)
root.mainloop()
´´´

我没有足够的声誉发表评论,但我相信我有答案@酷云是正确的,after(ms,func(将等待执行特定命令,而不会减慢其他tkinter功能的速度。在过去的一周里,我见证了这一点,构建了我的第一个Tkinter应用程序:(

无论何时你认为你想利用时间。睡眠,请使用:

root.after(ms, func)

最新更新