Python 3.x 我的tkinter代码经常崩溃,我如何使它更高效、更短?
我怎样才能使这段代码更高效、更短、重复更少 第一个问题是这些重复标签:我不知道如何缩短这些标签Python 3.x 我的tkinter代码经常崩溃,我如何使它更高效、更短?,python-3.x,tkinter,Python 3.x,Tkinter,我怎样才能使这段代码更高效、更短、重复更少 第一个问题是这些重复标签:我不知道如何缩短这些标签 label = tk.Label(root) label.pack() label2 = tk.Label(root) label2.pack() label3 = tk.Label(root) label3.pack() label4 = tk.Label(root) label4.pack() label5 = tk.Label(root) label5.pack() label6 =
label = tk.Label(root)
label.pack()
label2 = tk.Label(root)
label2.pack()
label3 = tk.Label(root)
label3.pack()
label4 = tk.Label(root)
label4.pack()
label5 = tk.Label(root)
label5.pack()
label6 = tk.Label(root)
label6.pack()
上述标签用于“清除功能”和“结果功能”:
def clear_widget():
widget = label
widget2 = label2
widget3 = label3
widget4 = label4
widget5 = label5
widget6 = label6
widget['text'] = ""
widget2['text'] = ""
widget3['text'] = ""
widget4['text'] = ""
widget5['text'] = ""
widget6['text'] = ""
if __name__ == '__main__':
button = tk.Button(root, text="Clear", command=clear_widget)
button.pack()
这是滚动功能:它用于滚动特定的次数,使用给定的百分比确定滚动的结果
def outcome():
answer = int(Percent.get().strip())
rolls = int(rollAmount.get().strip())
k = c = c3 = c4 = c5 = c6 = 0
#1 Roll
while True:
b = random.randrange(1,100)
new = 100 - answer
if b <= new:
label['text'] = "Fail."
k = k+1
if b >= new:
label['text'] = "Success!"
k = k+1
if k == rolls:
break
#2 Rolls
if rolls == 2:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
#3 Rolls
if rolls ==3:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
#4 Rolls
if rolls == 4:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
while True:
b4 = random.randrange(1,100)
new4 = 100 - answer
if b4 <= new4:
label4['text'] = "Fail."
c4 = c4+1
if b4 >= new4:
label4['text'] = "Success!"
c4 = c4+1
if c4 == rolls:
break
#5 Rolls
if rolls == 5:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
while True:
b4 = random.randrange(1,100)
new4 = 100 - answer
if b4 <= new4:
label4['text'] = "Fail."
c4 = c4+1
if b4 >= new4:
label4['text'] = "Success!"
c4 = c4+1
if c4 == rolls:
break
while True:
b5 = random.randrange(1,100)
new5 = 100 - answer
if b5 <= new5:
label5['text'] = "Fail."
c5 = c5+1
if b5 >= new5:
label5['text'] = "Success!"
c5 = c5+1
if c5 == rolls:
break
#6 Rolls
if rolls == 6:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
while True:
b4 = random.randrange(1,100)
new4 = 100 - answer
if b4 <= new4:
label4['text'] = "Fail."
c4 = c4+1
if b4 >= new4:
label4['text'] = "Success!"
c4 = c4+1
if c4 == rolls:
break
while True:
b5 = random.randrange(1,100)
new5 = 100 - answer
if b5 <= new5:
label5['text'] = "Fail."
c5 = c5+1
if b5 >= new5:
label5['text'] = "Success!"
c5 = c5+1
if c5 == rolls:
break
while True:
b6 = random.randrange(1,100)
new6 = 100 - answer
if b6 <= new6:
label6['text'] = "Fail."
c6 = c6+1
if b6 >= new6:
label6['text'] = "Success!"
c6 = c6+1
if c6 == rolls:
break
def outcome():
answer=int(百分比.get().strip())
rolls=int(rollmount.get().strip())
k=c=c3=c4=c5=c6=0
#一卷
尽管如此:
b=随机随机随机范围(1100)
新=100-答案
如果b=新的:
标签['text']=“成功!”
k=k+1
如果k==转鼓:
打破
#2卷
如果转鼓=2:
尽管如此:
b2=随机随机随机范围(1100)
new2=100-答案
如果b2=new2:
label2['text']=“成功!”
c=c+1
如果c==转鼓:
打破
#3卷
如果转鼓=3:
尽管如此:
b2=随机随机随机范围(1100)
new2=100-答案
如果b2=new2:
label2['text']=“成功!”
c=c+1
如果c==转鼓:
打破
尽管如此:
b3=随机随机随机范围(1100)
new3=100-答案
如果b3=new3:
label3['text']=“成功!”
c3=c3+1
如果c3==转鼓:
打破
#4卷
如果转鼓=4:
尽管如此:
b2=随机随机随机范围(1100)
new2=100-答案
如果b2=new2:
label2['text']=“成功!”
c=c+1
如果c==转鼓:
打破
尽管如此:
b3=随机随机随机范围(1100)
new3=100-答案
如果b3=new3:
label3['text']=“成功!”
c3=c3+1
如果c3==转鼓:
打破
尽管如此:
b4=随机随机随机范围(1100)
new4=100-答案
如果b4=new4:
label4['text']=“成功!”
c4=c4+1
如果c4==转鼓:
打破
#5卷
如果转鼓=5:
尽管如此:
b2=随机随机随机范围(1100)
new2=100-答案
如果b2=new2:
label2['text']=“成功!”
c=c+1
如果c==转鼓:
打破
尽管如此:
b3=随机随机随机范围(1100)
new3=100-答案
如果b3=new3:
label3['text']=“成功!”
c3=c3+1
如果c3==转鼓:
打破
尽管如此:
b4=随机随机随机范围(1100)
new4=100-答案
如果b4=new4:
label4['text']=“成功!”
c4=c4+1
如果c4==转鼓:
打破
尽管如此:
b5=随机随机随机范围(1100)
new5=100-答案
如果b5=new5:
标签5['text']=“成功!”
c5=c5+1
如果c5==转鼓:
打破
#6卷
如果转鼓=6:
尽管如此:
b2=随机随机随机范围(1100)
new2=100-答案
如果b2=new2:
label2['text']=“成功!”
c=c+1
如果c==转鼓:
打破
尽管如此:
b3=随机随机随机范围(1100)
new3=100-答案
如果b3=new3:
label3['text']=“成功!”
c3=c3+1
如果c3==转鼓:
打破
尽管如此:
b4=随机随机随机范围(1100)
new4=100-答案
如果b4=new4:
label4['text']=“成功!”
c4=c4+1
如果c4==转鼓:
打破
尽管如此:
b5=随机随机随机范围(1100)
new5=100-答案
如果b5=new5:
标签5['text']=“成功!”
c5=c5+1
如果c5==转鼓:
打破
尽管如此:
b6=随机随机随机范围(1100)
new6=100-答案
如果b6=new6:
label6['text']=“成功!”
c6=c6+1
如果c6==转鼓:
打破
整个代码
import random
import tkinter as tk
root = tk.Tk()
root.title("Try code")
canvas1 = tk.Canvas(root, width = 400, height = 300)
canvas1.pack()
Percent = tk.Entry(root)
Percent.pack()
percentLabel = tk.Label(root, text ="What is the percent of success?")
percentLabel.pack()
rollAmount = tk.Entry(root)
rollAmount.pack()
percentLabel = tk.Label(root, text ="How many rolls are you throwing?")
percentLabel.pack()
label = tk.Label(root)
label.pack()
label2 = tk.Label(root)
label2.pack()
label3 = tk.Label(root)
label3.pack()
label4 = tk.Label(root)
label4.pack()
label5 = tk.Label(root)
label5.pack()
label6 = tk.Label(root)
label6.pack()
def outcome():
answer = int(Percent.get().strip())
rolls = int(rollAmount.get().strip())
k = c = c3 = c4 = c5 = c6 = 0
#1 Roll
while True:
b = random.randrange(1,100)
new = 100 - answer
if b <= new:
label['text'] = "Fail."
k = k+1
if b >= new:
label['text'] = "Success!"
k = k+1
if k == rolls:
break
#2 Rolls
if rolls == 2:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
#3 Rolls
if rolls ==3:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
#4 Rolls
if rolls == 4:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
while True:
b4 = random.randrange(1,100)
new4 = 100 - answer
if b4 <= new4:
label4['text'] = "Fail."
c4 = c4+1
if b4 >= new4:
label4['text'] = "Success!"
c4 = c4+1
if c4 == rolls:
break
#5 Rolls
if rolls == 5:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
while True:
b4 = random.randrange(1,100)
new4 = 100 - answer
if b4 <= new4:
label4['text'] = "Fail."
c4 = c4+1
if b4 >= new4:
label4['text'] = "Success!"
c4 = c4+1
if c4 == rolls:
break
while True:
b5 = random.randrange(1,100)
new5 = 100 - answer
if b5 <= new5:
label5['text'] = "Fail."
c5 = c5+1
if b5 >= new5:
label5['text'] = "Success!"
c5 = c5+1
if c5 == rolls:
break
#6 Rolls
if rolls == 6:
while True:
b2 = random.randrange(1,100)
new2 = 100 - answer
if b2 <= new2:
label2['text'] = "Fail."
c = c+1
if b2 >= new2:
label2['text'] = "Success!"
c = c+1
if c == rolls:
break
while True:
b3 = random.randrange(1,100)
new3 = 100 - answer
if b3 <= new3:
label3['text'] = "Fail."
c3 = c3+1
if b3 >= new3:
label3['text'] = "Success!"
c3 = c3+1
if c3 == rolls:
break
while True:
b4 = random.randrange(1,100)
new4 = 100 - answer
if b4 <= new4:
label4['text'] = "Fail."
c4 = c4+1
if b4 >= new4:
label4['text'] = "Success!"
c4 = c4+1
if c4 == rolls:
break
while True:
b5 = random.randrange(1,100)
new5 = 100 - answer
if b5 <= new5:
label5['text'] = "Fail."
c5 = c5+1
if b5 >= new5:
label5['text'] = "Success!"
c5 = c5+1
if c5 == rolls:
break
while True:
b6 = random.randrange(1,100)
new6 = 100 - answer
if b6 <= new6:
label6['text'] = "Fail."
c6 = c6+1
if b6 >= new6:
label6['text'] = "Success!"
c6 = c6+1
if c6 == rolls:
break
def clear_widget():
widget = label
widget2 = label2
widget3 = label3
widget4 = label4
widget5 = label5
widget6 = label6
widget['text'] = ""
widget2['text'] = ""
widget3['text'] = ""
widget4['text'] = ""
widget5['text'] = ""
widget6['text'] = ""
if __name__ == '__main__':
button = tk.Button(root, text="Clear", command=clear_widget)
button.pack()
button1 = tk.Button(text='Get Num', command=outcome)
button1.pack()
root.mainloop()
随机导入
将tkinter作为tk导入
root=tk.tk()
root.title(“Try-code”)
canvas1=tk.Canvas(根,宽=400,高=300)
画布1.pack()
百分比=传统条目(根)
百分之
percentLabel=tk.Label(root,text=“成功的百分比是多少?”)
percentLabel.pack()
rollAmount=tk.Entry(根)
rollmount.pack()
percentLabel=tk.Label(root,text=“你扔了多少卷?”)
percentLabel.pack()
label=tk.label(根)
label.pack()
label2=传统标签(根)
标签2.pack()
label3=传统标签(根)
标签3.pack()
label4=传统标签(根)
标签4.pack()
label5=传统标签(根)
标签5.pack()
label6=传统标签(根)
标签6.pack()
定义结果():
answer=int(百分比.get().strip())
rolls=int(rollmount.get().strip())
k=c=c3=c4=c5=c6=0
#一卷
尽管如此:
b=随机随机随机范围(1100)
新=100-答案
如果b=新的:
标签['text']=“成功!”
k=k+1
如果k==转鼓:
打破
#2卷
如果转鼓=2:
尽管如此:
b2=随机随机随机范围(1100)
new2=100-答案
如果b2=new2:
label2['text']=“成功!”
c=c+1
如果c==转鼓:
打破
#3卷
如果转鼓=3:
尽管如此:
b2=随机随机随机范围(1100)
新2=
result_labels = []
for x in range(6):
label = tk.Label(root)
label.pack()
result_labels.append(label)
def outcome():
answer = int(Percent.get().strip())
rolls = int(rollAmount.get().strip())
for x in range(rolls):
b = random.randrange(1,100)
new = 100 - answer
if b <= new:
result_labels[x]["text"] = "Fail."
if b >= new:
result_labels[x]["text"] = "Success!"
def clear_widget():
for x in result_labels:
x["text"] = ""