Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python


import tkinter
import random

class App(tkinter.Frame):
def __init__(self, master):
tkinter.Frame.__init__(self,master)
self.master=master
self.grid()
self.colors=(('blue', 'green', 'orange', 'red', 'yellow', 'Tan', 'Brown', 'light green'))
self.bttn1=tkinter.Button(self, text='Click Me!', bg=str(self.colors[1]),
command=self.change_color)
self.bttn2=tkinter.Button(self, text='Click Me!', bg=str(self.colors[0]),
command=self.change_color)
self.bttn3=tkinter.Button(self, text='Click Me!', bg=str(self.colors[1]),
command=self.change_color)
self.bttn4=tkinter.Button(self, text='Click Me!', bg=str(self.colors[2]),
command=self.change_color)
self.bttn5=tkinter.Button(self, text='Click Me!', bg=str(self.colors[3]),
command=self.change_color)
self.bttn1.grid(row = 2, column = 0, sticky = 'nw')
self.bttn2.grid(row = 2, column = 1, sticky = 'nw')
self.bttn3.grid(row = 2, column = 2, sticky = 'nw')
self.bttn4.grid(row = 2, column = 3, sticky = 'nw')
self.bttn5.grid(row = 2, column = 4, sticky = 'nw')
def change_color(self):
random.shuffle(colors)
if self.bttn_clicks + 1:
self.bttn1.configure(background=color)
if self.bttn_clicks + 2:
self.bttn2.configure(background=color)
if self.bttn_clicks + 3:
bself.bttn3.configure(background=color)
self.bttn4.configure(background=color)
self.bttn5.configure(background=color)
root=tkinter.Tk()
root.title('Color Button')
root.geometry('300x50')
app=App(root)
root.mainloop()

What I have tried:

1. I tried to use labels and random.shuffle.
2. then someone said to try and use Iterators(I might just be using it wrong because I am a beginner)
3. Now Im trying to use a string under a function.

I will take any soulotion even if It changes everything. I understand that I am a beginner and what I'm doing might not even be helping me.
Posted
Updated 13-Mar-16 0:50am

1 solution

Use 9.6. random — Generate pseudo-random numbers[^] to generate a set of random indices for your colours. Remember to seed the generator with a different value, e.g. date or time, to ensure you do not get the same set each time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900