Carregar no Enter para mudar de cor

from tkinter import *
import random
cores = ['red', 'blue', 'green', 'yellow', 'orange', 'purple']
root = Tk()
root.geometry("500x250")
root.resizable(0, 0)
root.config(bg="#d9ead3")
root.title("Carregar no Enter para mudar de cor")
cor_label =Label(text="Clique ou pressione Enter para mudar a cor de label",
font=("Arial", 15), relief=RAISED)
cor_label.place(relx=0.05,rely=0.15)

def mudar_cor(event=None):
cor_atual = cor_label.cget('bg')
nova_cor = random.choice(cores)
while nova_cor == cor_atual:
nova_cor = random.choice(cores)
cor_label.config(bg=nova_cor)
but_mudacor = Button(text="Mudar de cor", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=mudar_cor)
but_mudacor.place(relx=0.25, rely=0.55, relwidth=0.3, relheight=0.15)

but_sair = Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.65, rely=0.55, relwidth=0.25, relheight=0.15)
root.bind('<Return>', mudar_cor)
root.mainloop()

Comentários

Mensagens populares deste blogue

Criar Cartões de Visita

12 signos egípcios

Calcular a percentagem de ocupação