Python tkinter代码停止工作

时间:2017-03-04 19:42:04

标签: python python-2.7 python-3.x tkinter tkinter-canvas

我正在使用tkinter在python中处理轮盘赌代码。我很喜欢它。但问题是,经过几次劳斯后,它开始出现问题并崩溃。 (我知道它不是一个太好的代码,并且有很多不必要的步骤,我仍然是编程的新手)。

from tkinter import *
import tkinter.messagebox


root = Tk()
import random

money2 = StringVar()
varblack = StringVar()
vargreen = StringVar()
varred = StringVar()

numlist = [7]
newnumlist = [1]

def num():
    global numlist
    number = random.randint(1,15)
    if num.counter == 10:
       numlist.append(number)
    num.counter +=1
    return numlist[len(numlist) - 1]

def newnumone():
    global newnumlist
    newnumber = random.randint(1, 4)
    if newnumone.counter == 10:
       newnumlist.append(newnumber)
    newnumone.counter += 1
    return newnumlist[len(newnumlist) - 1]

newnumone.counter = 0   
num.counter = 0
RollList = ['red', 'black', 'red','black', 'red', 'black','red', 'green', 'black', 'red','black', 'red', 'black', 'red', 'black']

def colorsone():
    newList = RollList[::-1]
    if newList[0:9] == ['red', 'black', 'red','black', 'red', 'black','red', 'black', 'red']:
       RollList.append('green')
    if newList[0] == 'red':
      RollList.append('black')
    if newList[0] == 'black':
       RollList.append('red')

def lol():
    i = 0
    global RollList
    if lol.counter == 0:
       num.counter = 10
       newnumone.counter = 10
       RollList = RollList[0:15]
    number = int(num())
    newnum = int(newnumone())
    newRollsList = RollList[::-1]
    if newRollsList[0] == 'red':
       colorbgo1 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[0] == 'green':
       colorbgo1 = 'green'
       i = i + 1
#       colors('red')
    if newRollsList[0] == 'black':
       colorbgo1 = 'black'
       i = i + 1
#       colors('green')
    if newRollsList[1] == 'red':
       colorbgo2 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[1] == 'green':
       colorbgo2 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[1] == 'black':
       colorbgo2 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[2] == 'red':
       colorbgo3 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[2] == 'green':
       colorbgo3 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[2] == 'black':
       colorbgo3 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[3] == 'red':
       colorbgo4 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[3] == 'green':
       colorbgo4 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[3] == 'black':
       colorbgo4 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[4] == 'red':
       colorbgo5 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[4] == 'green':
       colorbgo5 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[4] == 'black':
       colorbgo5 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[5] == 'red':
       colorbgo6 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[5] == 'green':
       colorbgo6 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[5] == 'black':
       colorbgo6 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[6] == 'red':
       colorbgo7 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[6] == 'green':
       colorbgo7 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[6] == 'black':
       colorbgo7 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[7] == 'red':
       colorbgo8 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[7] == 'green':
       colorbgo8 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[7] == 'black':
       colorbgo8 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[8] == 'red':
       colorbgo9 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[8] == 'green':
       colorbgo9 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[8] == 'black':
       colorbgo9 = 'black'
       i = i + 1
#       colors('black')
    if newRollsList[9] == 'red':
       colorbgo10 = 'red'
       i = i + 1
#       colors('black')
    if newRollsList[9] == 'green':
       colorbgo10 = 'green'
       i = i + 1
#       colors('black')
    if newRollsList[9] == 'black':
       colorbgo10 = 'black'
       i = i + 1
#       colors('black')
    lol.counter += 1
    colorsone()
    if lol.counter < 150:
       countdownone(1, 10)
    if lol.counter < 200 and lol.counter > 149:
       countdownone(1, 40)
    if lol.counter < 230 and lol.counter > 199:
       countdownone(1, 80)
    if lol.counter < 240 and lol.counter >229:
       countdownone(1, 140)
    if lol.counter < 250 and lol.counter > 239:
       countdownone(1, 200)
    if number == 8: 
       if lol.counter < 258 and lol.counter > 249:
          countdownone(1, 300)
       if lol.counter < 259 and lol.counter > 257:
          countdownone(1, 500)
       if lol.counter == 259:
          play()
#             lol.counter = 0   
    if number >= 9: #black    
       if newnum == 1:
          if lol.counter < 254 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 257 and lol.counter > 253:
             countdownone(1, 500)
          if lol.counter == 257:
#             print(1)
             play()
#             lol.counter = 0   
       if newnum == 2:
          if lol.counter < 252 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 255 and lol.counter > 251:
             countdownone(1, 500)
          if lol.counter == 255:
#             print(2)
             play()
#             lol.counter = 0   
       if newnum == 3:
          if lol.counter < 259 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 262 and lol.counter > 258:
             countdownone(1, 500) 
          if lol.counter == 262:
#             print(3)
             play()
#             lol.counter = 0   
       if newnum == 4:
          if lol.counter < 258 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 260 and lol.counter > 257:
             countdownone(1, 500)   
          if lol.counter == 260:
#             print(4)
             play()
#             lol.counter = 0   
    if number <= 7: #red
       if newnum == 1:
          if lol.counter < 254 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 256 and lol.counter > 253:
             countdownone(1, 500)
          if lol.counter == 256:
#             print(5)
             play()
#             lol.counter = 0   
       if newnum == 2:
          if lol.counter < 252 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 253 and lol.counter > 251:
             countdownone(1, 500)
          if lol.counter == 253:
             play()
#             print(6)
#             lol.counter = 0   
       if newnum == 3:
          if lol.counter < 256 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 258 and lol.counter > 255:
             countdownone(1, 500) 
          if lol.counter == 258:
#             print(7)
             play()
#             lol.counter = 0   
       if newnum == 4:
          if lol.counter < 258 and lol.counter > 249:
             countdownone(1, 300)
          if lol.counter < 261 and lol.counter > 257:
             countdownone(1, 500)   
          if lol.counter == 261:
#             print(8)
             play()
#            lol.counter = 0             

    labelRoll1 = Label(root, bg = colorbgo1, height=4, width = 8).grid(row = 10, column = 4)
#    labelRollsw1 = Label(root, text = ' ').grid(row = 15, column = 4)
    labelRoll2 = Label(root, bg = colorbgo2, height=4, width = 8).grid(row = 10, column = 5)
#    labelRollsw2 = Label(root, text = ' ').grid(row = 15, column = 5)
    labelRoll3 = Label(root, bg = colorbgo3, height=4, width = 8).grid(row = 10, column = 6)
#    labelRollsw3 = Label(root, text = ' ').grid(row = 15, column = 6)
    labelRoll4 = Label(root, bg = colorbgo4, height=4, width = 8).grid(row = 10, column = 7)
#    labelRollsw4 = Label(root, text = ' ').grid(row = 15, column = 7)
    labelRoll5 = Label(root, bg = colorbgo5, height=4, width = 8).grid(row = 10, column = 8)
#    labelRollsw5 = Label(root, text = ' ').grid(row = 15, column = 8)
    labelRoll6 = Label(root, bg = colorbgo6, height=4, width = 8).grid(row = 10, column = 9)
#    labelRollsw6 = Label(root, text = ' ').grid(row = 15, column = 9)
    labelRoll7 = Label(root, bg = colorbgo7, height=4, width = 8).grid(row = 10, column = 10)
#    labelRollsw7 = Label(root, text = ' ').grid(row = 15, column = 10)
    labelRoll8 = Label(root, bg = colorbgo8, height=4, width = 8).grid(row = 10, column = 11)
#    labelRollsw8 = Label(root, text = ' ').grid(row = 15, column = 11)
    labelRoll9 = Label(root, bg = colorbgo9, height=4, width = 8).grid(row = 10, column = 12)
#    labelRollsw9 = Label(root, text = ' ').grid(row = 15, column = 12)
    labelRoll10 = Label(root, bg = colorbgo10, height=4, width = 8).grid(row = 10, column = 13)


labelRollsw1 = Label(root, height=4, width = 8).grid(row = 17, column = 4)
labelRollsw2 = Label(root, height=4, width = 8).grid(row = 17, column = 5)
labelRollsw3 = Label(root, height=4, width = 8).grid(row = 17, column = 6)
labelRollsw4 = Label(root, height=4, width = 8).grid(row = 17, column = 7)
labelRollsw5 = Label(root, height=4, width = 8).grid(row = 17, column = 8)
labelRollsw6 = Label(root, height=4, width = 8).grid(row = 17, column = 9)
labelRollsw7 = Label(root, height=4, width = 8).grid(row = 17, column = 10)
labelRollsw8 = Label(root, height=4, width = 8).grid(row = 17, column = 11)
labelRollsw9 = Label(root, height=4, width = 8).grid(row = 17, column = 12)
labelRollsw10 = Label(root, height=4, width = 8).grid(row = 17, column = 13)    
labelRollsw11 = Label(root, height=4, width = 8).grid(row = 17, column = 14)

lol.counter = 0

def countdownone(count, i): 
#    timer['text'] = count
    if count > 0:
       root.after(i, countdownone, count-count, i)
    if count == 0:
       lol()


#timerone = Label(root)
#timerone.grid()

end = Label(root, text = '↓').grid(row = 8, column =9)
endone = Label(root, text = '↑').grid(row = 16, column =9)
#allin = Button(root, text = 'LOL', command = countdown(1, 100)).grid(row = 3, column = 3)


def countdown(count):    
    timer['text'] = count
    if count > 0:
       root.after(1000, countdown, count-1)
    if count == 3:
       countdownone(1, 10)
    if count == 20:
       entry_money.configure(state="normal")
       greenB.configure(state="normal")
       blackB.configure(state="normal")
       redB.configure(state="normal")
    if count == 0:
       entry_money.configure(state="disabled")
       greenB.configure(state="disabled")
       blackB.configure(state="disabled")
       redB.configure(state="disabled")
#   if count == 0:
#       play()


def play():
    varblack1 = varblack.get()
    vargreen1 = vargreen.get()
    varred1 = varred.get()
    if vargreen1 == 'off' and varblack1 == 'off' and varred1 == 'off':
       nothing()
    if varred1 == 'on':
#       blackB.deselect()
#       greenB.deselect()
       red()
    if varblack1 == 'on':
#       greenB.deselect()
       black()
    if vargreen1 == 'on':
       green()
    redB.deselect()
    blackB.deselect()
    greenB.deselect()
    countdown(20)
    lol.counter = 0
    entry_money.delete(0, 'end')
#    newnumone.counter = 0  
#    num.counter = 0

def all():
    total1=total.get()
    money2.set(total1)  

def red():
    money1 = money2.get()
    roulette3('red', money1)

def black():
    money1 = money2.get()
    roulette3('black', money1)

def green():
    money1 = money2.get()
    roulette3('green', money1)

def nothing():
    roulette3('no', 1)


def colors(clr):
    file = open('newfile.txt', 'a')
    file.write(clr)
    file.close()
    file = open('newfile.txt', 'r')
    file.close()
    PrevRollsColor()



def PrevRollsColor():
    lastRollsList = []
    lastRolls = open("newfile.txt", "r")
    Rolls = lastRolls.read().splitlines()
    for line in Rolls:
      lastRollsList.append(line)
    lastRolls.close()
    newRollsList = lastRollsList[::-1]
    #labelRollsList = [labelRolls2, labelRolls3, labelRolls4, labelRolls5, labelRolls6, labelRolls7, labelRolls8, labelRolls9, labelRolls10]
    if newRollsList[0] == 'red':
       colorbg1 = 'red'
    if newRollsList[0] == 'green':
       colorbg1 = 'green'
    if newRollsList[0] == 'black':
       colorbg1 = 'black'
    if newRollsList[1] == 'red':
       colorbg2 = 'red'
    if newRollsList[1] == 'green':
       colorbg2 = 'green'
    if newRollsList[1] == 'black':
       colorbg2 = 'black'
    if newRollsList[2] == 'red':
       colorbg3 = 'red'
    if newRollsList[2] == 'green':
       colorbg3 = 'green'
    if newRollsList[2] == 'black':
       colorbg3 = 'black'
    if newRollsList[3] == 'red':
       colorbg4 = 'red'
    if newRollsList[3] == 'green':
       colorbg4 = 'green'
    if newRollsList[3] == 'black':
       colorbg4 = 'black'
    if newRollsList[4] == 'red':
       colorbg5 = 'red'
    if newRollsList[4] == 'green':
       colorbg5 = 'green'
    if newRollsList[4] == 'black':
       colorbg5 = 'black'
    if newRollsList[5] == 'red':
       colorbg6 = 'red'
    if newRollsList[5] == 'green':
       colorbg6 = 'green'
    if newRollsList[5] == 'black':
       colorbg6 = 'black'
    if newRollsList[6] == 'red':
       colorbg7 = 'red'
    if newRollsList[6] == 'green':
       colorbg7 = 'green'
    if newRollsList[6] == 'black':
       colorbg7 = 'black'
    if newRollsList[7] == 'red':
       colorbg8 = 'red'
    if newRollsList[7] == 'green':
       colorbg8 = 'green'
    if newRollsList[7] == 'black':
       colorbg8 = 'black'
    if newRollsList[8] == 'red':
       colorbg9 = 'red'
    if newRollsList[8] == 'green':
       colorbg9 = 'green'
    if newRollsList[8] == 'black':
       colorbg9 = 'black'
    if newRollsList[9] == 'red':
       colorbg10 = 'red'
    if newRollsList[9] == 'green':
       colorbg10 = 'green'
    if newRollsList[9] == 'black':
       colorbg10 = 'black'


    labelRolls1 = Label(root, bg = colorbg1, height = 1, width = 2).grid(row = 18, column = 16)
    labelRollsw1 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 17)
    labelRolls2 = Label(root, bg = colorbg2, height = 1, width = 2).grid(row = 18, column = 18)
    labelRollsw2 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 19)
    labelRolls3 = Label(root, bg = colorbg3, height = 1, width = 2).grid(row = 18, column = 20)
    labelRollsw3 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 21)
    labelRolls4 = Label(root, bg = colorbg4, height = 1, width = 2).grid(row = 18, column = 22)
    labelRollsw4 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 23)
    labelRolls5 = Label(root, bg = colorbg5, height = 1, width = 2).grid(row = 18, column = 24)
    labelRollsw5 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 25)
    labelRolls6 = Label(root, bg = colorbg6, height = 1, width = 2).grid(row = 18, column = 26)
    labelRollsw6 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 27)
    labelRolls7 = Label(root, bg = colorbg7, height = 1, width = 2).grid(row = 18, column = 28)
    labelRollsw7 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 29)
    labelRolls8 = Label(root, bg = colorbg8, height = 1, width = 2).grid(row = 18, column = 30)
    labelRollsw8 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 31)
    labelRolls9 = Label(root, bg = colorbg9, height = 1, width = 2).grid(row = 18, column = 32)
    labelRollsw9 = Label(root, bg = 'white', text = ' ').grid(row = 18, column = 33)
    labelRolls10 = Label(root, bg = colorbg10, height = 1, width = 2).grid(row = 18, column = 34)   

def roulette3(color, money):
    yes = 'yes'
    no = 'no'
    on = 'yes'
    total_money = total.get()
    number = int(num())
    try:
        if color == no and money == 1:
          if number == 8:
            colors('green\n')
          if number <= 7:
            colors('red\n')
          if number >= 9:
            colors('black\n')
        if int(money) <= int(total_money) and int(money) > 0:
          if color == 'green' or color == 'red' or color == 'black':
            if number == 8:
              if color == 'green':
                total.set(int(total_money) + int(money) * 14 - int(money))
                a = ('It was green! You won!')
#                tkinter.messagebox.showinfo('You won', a)
                colors('green\n')
              elif color != 'green':
                total.set(int(total_money)  - int(money))
                b = ('It was green! You lost!')
#                tkinter.messagebox.showinfo('You lost', b)
                colors('green\n')
            if number <= 7:
              if color == 'red':
                total.set(int(total_money) + int(money) * 2 - int(money))
                c = ('It was red! You won!')
#                tkinter.messagebox.showinfo('You won', c)
                colors('red\n')
              elif color != 'red':
                total.set(int(total_money) - int(money))
                d = ('It was red! You lost!')
#                tkinter.messagebox.showinfo('You lost', d)
                colors('red\n')
            if number >= 9:
              if color == 'black':
                total.set(int(total_money) + int(money) * 2 - int(money))
                e = ('It was black! You won!')
#                tkinter.messagebox.showinfo('You won', e)
                colors('black\n')
              elif color != 'black':
                total.set(int(total_money) - int(money))
                f = ('It was black! You lost!')
#                tkinter.messagebox.showinfo('You lost', f)
                colors('black\n')
        else:
            g = ('you are unable to play with this amount')
#            tkinter.messagebox.showinfo('Wrong Amount', g)
            nothing()
    except:
        tkinter.messagebox.showinfo('Invalid Input', 'Invalid Input')
        nothing()


total = StringVar()
total.set('1000')
yourMoney = Label(root, text = 'Your Money:')
yourMoney.grid(row = 1, column = 0)

all_money = Label(root, textvariable = total)   
all_money.grid(row = 1, column = 1)


roulette = Label(root, text = 'Roulette', bg = 'red', fg = 'black')
roulette.grid(row = 0, column = 1, sticky = N)


moneyLabel = Label(root, text = 'How much:')
entry_money = Entry(root, textvariable = money2)


moneyLabel.grid(row = 2, column = 1)
entry_money.grid(row = 3, column = 1)

selectcolor = Label(root, text = 'Select your color:')
selectcolor.grid(row = 5, column = 0)

redB = Checkbutton(root, text = 'Red', variable = varred, onvalue = 'on', offvalue = 'off')
redB.grid(row = 4, column = 1)
blackB = Checkbutton(root, text = 'Black', variable = varblack, onvalue = 'on', offvalue = 'off')
blackB.grid(row = 5, column = 1)
greenB = Checkbutton(root, text = 'Green', variable = vargreen, onvalue = 'on', offvalue = 'off')
greenB.grid(row = 6, column = 1)

#playButton = Button(root, text = 'Play', command = play).grid(row = 4, column = 5)

#dontroll = Button(root, text = 'Wait for next Roll', command = nothing).grid(row = 3, column = 3)
lastrolls = Label(root, text = 'Last Rolls:')
lastrolls.grid(row = 18, column = 15)

zeit = Label(root, text = 'Time left until next roll:').grid(row = 1, column = 3)
timer = Label(root)
timer.grid(row = 1, column = 4)

allin = Button(root, text = 'All In', command = all).grid(row = 3, column = 3)




blackB.deselect()
greenB.deselect()
redB.deselect()

PrevRollsColor()

countdown(20)

#photo = PhotoImage(file = 'C:\\Users\\Louis\\Desktop\\roulette.png')
#label = Label(root, image=photo)
#label.grid(row = 7, column = 1)


root.geometry('1500x500+500+300')
root.title('roulette')
root.mainloop()

0 个答案:

没有答案