[英]time.sleep makes tkinter crash is there anything else i can use
So I'm making a case opener based on CS:GO's odds and I'm trying to make an animation of some sort using random number generation.所以我正在制作一个基于 CS:GO 赔率的案例开场白,我正在尝试使用随机数生成制作某种动画。
Example:例子:
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)
But if I add time.sleep()
to make the animation seem like its rolling tkinter just crashes.但是如果我添加
time.sleep()
使动画看起来就像它的滚动 tkinter 只是崩溃了。 So is there anything else I can use to make it wait.那么还有什么我可以用来让它等待的吗? x just says how many times its gonna do the animation and z is what the time is going to be but I haven't set up the right multiplication yet to make it right.
x 只是说它要做多少次动画,而 z 是时间将会是多少,但我还没有设置正确的乘法来使它正确。
all the code所有的代码
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()
´´´
I do not have enough reputation to comment, but I believe I have the answer.我没有足够的声誉来发表评论,但我相信我有答案。 @Cool Cloud is correct that after(ms, func) will wait to execute a specific command without slowing down other tkinter functionality.
@Cool Cloud 是正确的 after(ms, func) 将等待执行特定命令而不会减慢其他 tkinter 功能。 I have witnessed this over the past week building my first Tkinter application :)
在过去的一周里,我亲眼目睹了构建我的第一个 Tkinter 应用程序:)
Whenever you think you want to use time.sleep(s), use instead:每当您认为要使用 time.sleep(s) 时,请改用:
root.after(ms, func)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.