如何在“if”循环中使用我的列表?

2024-09-28 01:30:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用ifif中的列表创建一个新窗口,首先,我的列表是空的,但是当我们单击button1button2button3时,它会添加颜色列表的值,如果它是“黄色”、“红色”或“绿色”,在这个程序中,我的目标是在3次尝试中有2个颜色球,但是当我运行程序时,它会说我的列表“超出范围”(回溯(最近一次呼叫): 文件“C:/Users/Arnold Tchiasso/Desktop/g2.py”,第91行,in 如果liste[0]==liste[1]: 索引器:列表索引超出范围)我向您展示:

import tkinter as tk
from random import shuffle

fenetre = tk.Tk()
fenetre['bg']='black'
fenetre.geometry("1152x768")
color = ["red", "green", "yellow"]
shuffle(color)
liste=[]

frameGauche = tk.Frame(width=200, height=600, bg='pink')
frameGauche.grid(row=0, column=0, padx=10, pady=10)

frameDroite = tk.Frame(width=700, height=700, bg='grey')
frameDroite.grid(row=0, column=1, padx=10, pady=10)

portegauche=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portegauche.grid(row=0, column=0, padx=5, pady=5)

portemilieu=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portemilieu.grid(row=0, column=1, padx=5, pady=5)

portedroite=tk.Frame(frameDroite, width=200, 
height=600,bg='white',bd=5,relief='groove')
portedroite.grid(row=0, column=2, padx=5, pady=5)

canvas1=tk.Canvas(portegauche,width=200, height=600, bg='white')
c1 = canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
canvas1.grid_forget()

canvas2=tk.Canvas(portemilieu,width=200, height=600, bg='white')
c2 = canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
canvas2.grid_forget()

canvas3=tk.Canvas(portedroite,width=200, height=600, bg='white')
c3 = canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
canvas3.grid_forget()

def normalize_button(btn_name):
    print(btn_name)
    if btn_name == "b1":
        bouton1['state']='normal'
    if btn_name == "b2":
        bouton2['state']='normal'
    if btn_name == "b3":
        bouton3['state']='normal'

def show1():
    canvas1.grid(row=0, column=1)
    bouton2['state']='disabled'
    bouton3['state']='disabled'
    fenetre.after(2000, normalize_button, "b2")
    fenetre.after(2000, normalize_button, "b3")
    fenetre.after(2000, canvas1.grid_forget)
    shuffle(color)
    liste.append(color[0])
    print(liste)

def show2():
    canvas2.grid(row=0, column=2)
    bouton1['state']='disabled'
    bouton3['state']='disabled'
    fenetre.after(2000, normalize_button, "b1")
    fenetre.after(2000, normalize_button, "b3")
    fenetre.after(2000, canvas2.grid_forget)
    shuffle(color)
    liste.append(color[1])
    print(liste)

def show3():
    canvas3.grid(row=0, column=3)
    bouton2['state']='disabled'
    bouton1['state']='disabled'
    fenetre.after(2000, normalize_button, "b2")
    fenetre.after(2000, normalize_button, "b1")
    fenetre.after(2000, canvas3.grid_forget)
    shuffle(color)
    liste.append(color[2])
    print(liste)

def recommencer():
    canvas1.grid_remove()
    canvas2.grid_remove()
    canvas3.grid_remove()
    shuffle(color)
    canvas1.create_oval((60,280), (140,340), width=1, outline="black", fill=color[0])
    canvas2.create_oval((60,280), (140,340), width=1, outline="black", fill=color[1])
    canvas3.create_oval((60,280), (140,340), width=1, outline="black", fill=color[2])
    bouton1['state']='normal'
    bouton2['state']='normal'
    bouton3['state']='normal'

if liste[0]==liste[1]:
    top = tk.Toplevel()
    top.title("C'est gagné")
    lab1=tk.Label(top, text="WIN")
if liste[0]==liste[2]:
    top = tk.Toplevel()
    top.title("C'est gagné")
    lab1=tk.Label(top, text="WIN")
if liste[1]==liste[2]:
    top = tk.Toplevel()
    top.title("C'est gagné")
    lab1=tk.Label(top, text="WIN")

boutonR = tk.Button(frameGauche, text='Recommencer',command=recommencer)
boutonR.grid(row=0, column=0, padx=50, pady=50)

bouton1=tk.Button(frameDroite, text= 'Ouvrir',command=show1)
bouton1.grid(row=1, column=0)

bouton2=tk.Button(frameDroite, text= 'Ouvrir',command=show2)
bouton2.grid(row=1, column=1)

bouton3=tk.Button(frameDroite, text= 'Ouvrir',command=show3)
bouton3.grid(row=1, column=2)



fenetre.mainloop()

Tags: iftopcolumnwidthtkgridcolorrow

热门问题