how to create colour changing wall app using python and tkinter

Namaste Friends,



In Today's Tutorial I am Going To Show You How To Create Colour Changing Wall App Using Python And Tkinter

So, Let's Start

Friends, In This App We Will Use Python Tkinter Canvas Widget As A Colour Wall To Make This App More Easy And Fast And Tkinter Binding Functions For Binding Our Colour Changing Function With Mouse Motion.

Now, Let see Our Codes.


WallColour.py


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# import module
import Tkinter
import random

# RGB Color Selecting Function
def rgb(x,y,z):
 return "#%02x%02x%02x" % (x,y,z)

# Random Color Chooser
def random_color():
 x = random.randint(1,255)
 y = random.randint(1,255)
 z = random.randint(1,255)
 return rgb(x,y,z)


# Some Configurations
WIDTH=25
HEIGHT=25
GRID_W = 20
GRID_H = 20

# Main Class For Canvas Widget
class Wall(Tkinter.Canvas):
 def __init__(self, *args, **kwargs):
  Tkinter.Canvas.__init__(self, *args, **kwargs)
  self.squares = []
  self.create_squares()

  # binding mouse motion
  self.bind("<Motion>", self.change)

 # Create Squares
 def create_squares(self):
  for i in range(GRID_W):
   for j in range(GRID_H):
    x1 = i*WIDTH
    y1 = j*HEIGHT
    x2 = x1+WIDTH
    y2 = y1+HEIGHT
    s=self.create_rectangle(x1,y1,x2,y2, fill=random_color(), tag="{}{}".format(i,j))
    self.squares.append(s)
  return

 # Change Colors 
 def change(self, event=None):
  for i in self.squares:
   self.itemconfig(i, fill=random_color())
  return


# main Function
def main():
 root=Tkinter.Tk(className=" Color Wall")
 k=Wall(root, width=300, height=200)
 k.pack(expand=True, fill="both")
 root.mainloop()
 return

# Main Trigger
if __name__=="__main__":
 main()


As You Can See In Above Codes. In First Part, I Am Importing Module And In Second And Third Part I Am Create Simple Functions For Choosing Random Colours For Our Colour Wall. After That Class Wall Is For Handling All Canvas Related Functions and main function is for creating root window. Done!


In Simple Words, First We Will Create Grid Of Squares In Plain Canvas Text And Then We Will Bind Our Mouse Motion Triggers with Random Colour Choosing Function Located In Line No. 46 After Running This App, Whenever We will Move Our Mouse In Canvas All Colour Of Grid Will Randomly Change.

This Tutorial Ends Here.


Thanks For Reading
Have A Nice Day
SSB



Share this

Related Posts

Previous
Next Post »