单击按钮时,Python Tkinter程序会冻结

时间:2018-11-21 13:41:09

标签: python tkinter

我是tkinter的新手,我正在尝试制作老虎机程序。自从我在这里添加了功劳部分之后:

credit=100
while slot1 != "skull.gif" and slot2 !="skull.gif" or slot2 !="skull.gif" and slot3!="skull.gif":
    if slot1 == slot2 and slot2 == slot3:
        if slot1=="bell.gif" and slot2=="bell.gif" and slot3=="bell.gif":
            credit=credit+500
            update()
        else:
            credit=credit+100
            update()
    elif slot1 == slot2 or slot2 == slot3:
        credit=credit+50
        update()
if slot1 =="skull.gif" and slot2=="skull.gif" or slot2=="skull.gif" and slot3=="skull.gif":
    credit=credit-100
    update()
elif slot1 =="skull.gif" and slot2=="skull.gif" and slot3=="skull,gif":
    credit=0
    update() 

def update():
    global convert
    global root
    global creditlabel
    global credit
    convert.configure("CREDIT: "+ str(credit))
    root.update()
    root.after(500, update)

当我单击滚动按钮时,程序冻结。我尝试使用.config和.set,但是同一件事不断发生,并且在滚动时似乎无法更新信用额。 任何帮助将不胜感激。

整个程序:

import tkinter
import random
import os
from tkinter import *
def play():
    global credit
    slot1=(random.choice(fruit))
    slot2=(random.choice(fruit))
    slot3=(random.choice(fruit))
    slot1c=PhotoImage(file=os.path.realpath(slot1))
    slot2c=PhotoImage(file=os.path.realpath(slot2))
    slot3c=PhotoImage(file=os.path.realpath(slot3))
    slot1label=Label(root,image=slot1c)
    slot2label=Label(root,image=slot2c)
    slot3label=Label(root,image=slot3c)
    slot1label.image=slot1c
    slot2label.image=slot2c
    slot3label.image=slot3c
    slot1label.grid(row=1, column=0, columnspan=1, rowspan=1)
    slot2label.grid(row=1, column=1, columnspan=1, rowspan=1)
    slot3label.grid(row=1, column=2, columnspan=1, rowspan=1,padx=120)
    credit=100
    while slot1 != "skull.gif" and slot2 !="skull.gif" or slot2 !="skull.gif" and slot3!="skull.gif":
        if slot1 == slot2 and slot2 == slot3:
            if slot1=="bell.gif" and slot2=="bell.gif" and slot3=="bell.gif":
                credit=credit+500
                update()
            else:
                credit=credit+100
                update()
        elif slot1 == slot2 or slot2 == slot3:
            credit=credit+50
            update()
    if slot1 =="skull.gif" and slot2=="skull.gif" or slot2=="skull.gif" and slot3=="skull.gif":
        credit=credit-100
        update()
    elif slot1 =="skull.gif" and slot2=="skull.gif" and slot3=="skull,gif":
        credit=0
        update()
def update():
    global convert
    global root
    global creditlabel
    global credit
    convert.configure("CREDIT: "+ str(credit))
    root.update()
    root.after(500, update)

global convert
global root
global creditlabel
root = tkinter.Tk()
root.geometry("1236x570")
root.title("Slot Machine")
root.configure(background='#BE28BF')
title= PhotoImage(file=os.path.realpath("fruitslots.gif"))
titleimgLabel = Label(root,image=title)
titleimgLabel.grid(row=0, column=0,rowspan=1,padx=5,pady=5, sticky=W)
fruit=["cherry.gif","bell.gif","lemon.gif","orange.gif","star.gif","skull.gif"]

convert=tkinter.IntVar()
creditlabel= Label(root, textvariable=convert, fg="#00a8fc", bg="#BE28BF",  borderwidth=0, font=('Hobo Std', '28'), text="CREDIT: 100")
creditlabel.grid(row=0, column=1, sticky=NW, pady=5)
convert.set("CREDIT: 100")

quitButton = Button(root, text='QUIT', fg='White',bg='Red',borderwidth=0,font=('Hobo Std', '28'), command=root.destroy)
quitButton.grid(row=3,column=2,padx=10, pady=10, sticky=E)

rollButton = Button(root, text='ROLL', command=play, bg='lime', borderwidth=0, font=('Hobo Std', '28'))
rollButton.grid(row=3,column=0, padx=5, pady=10, sticky=W)
root.mainloop()

0 个答案:

没有答案