-The screensaver can be started by itself, or it can be started manually (use manual start Here)
-Stops when the mouse is moved, or if other events are raised
-If the screensaver is a canvas, there is no frame
-The action of the image is random, has randomness, may include color, size, number, direction of movement, etc.
-Overall Composition:
-ScreenSaver:
-Requires a canvas, size consistent with screen, no border
-Ball
-Random color, size, number, direction of movement, etc.
-The ball is active by calling
1 __author__="Qiuxirufeng"2 3 ImportRandom4 ImportTkinter5 6 7 classRandromball ():8 " "9 class defining the ball of motionTen " " One A def __init__(self, canvas, Scrnwidth, scrnheight): - " " - Canvas : Canvases, all content should be rendered on the canvas, where this variable is passed in the scrnwidth/scrnheight: Screen size, width and height - " " - #Initializing the canvas -Self.canvas =Canvas + - #The initial position of the ball appears randomly, where the position represents the center of the ball + #Xpos, ypos represents the coordinates where the position appears ASelf.xpos = Random.randint (ten, int (scrnwidth)-20) atSelf.ypos = Random.randint (ten, int (scrnheight)-20) - - #define the speed of the ball motion - #simulate motion: Constantly erase the original picture and redraw it in a new place - #here the xvelocity simulates the x-axis direction of motion -self.xvelocity = Random.randint (4, 20) in #Likewise, the yvelocity simulates the y-axis motion. -self.yvelocity = Random.randint (4, 20) to + #define the size of the screen -Self.scrnwidth =Scrnwidth theSelf.scrnheight =Scrnheight * $ #the size of the ball is randomPanax Notoginseng #here the size of the ball is represented by the radius -Self.radius = Random.randint (20, 120) the + #Define Color A #RGB notation: Three numbers, each with a value of 0-255, representing the size of the red, green, and blue three colors the #in some systems, it can also be expressed directly in English words, such as red, green + #The RGB notation is used here, and a lambda expression is used to find an anonymous function for RGB three random values. -c =Lambda: Random.randint (0, 255) $Self.color ="#%02x%02x%02x"%(c (), C (), C ()) $ - defCreate_ball (self): - " " the A variable value defined with a constructor, a ball is drawn on the Zaicanvas - " "Wuyi the #Tkinter does not draw a circular function, only the function of the ellipse, the ellipse needs to be defined two coordinates - #in a rectangular inner-painted ellipse, you just need to define the upper-left and lower-right corners of the rectangle. Wu #The method for two coordinates is that the coordinates of the center of the circle are known, and the center coordinates minus the radius can find the upper-left coordinate, and the radius can find the coordinates of the lower right angle. -X1 = Self.xpos-Self.radius AboutY1 = Self.ypos-Self.radius $x2 = Self.xpos +Self.radius -y2 = Self.ypos +Self.radius - - #with two diagonal coordinates, it is possible to draw a circular operation A #Fill indicates the padding color + #outline is the perimeter border color of the circle theSelf.item = Self.canvas.create_oval (x1, y1, x2, y2, Fill=self.color, outline=Self.color) - $ defMove_ball (self): the #to move the ball, you need to control the direction of the ball. the #after each move, the ball has a new coordinate theSelf.xpos + =self.xvelocity theSelf.ypos + =self.yvelocity - in #The following determines if the ball will wall the ifSelf.xpos + Self.radius >= self.scrnwidthorSelf.xpos-self.radius <=0: the #Hit the right wall on the left, the ball is in the opposite direction, X is negative . AboutSelf.xvelocity =-self.xvelocity the #or add a minus sign to x with the next line of code. the #self.xvelocity *=-1 the #In the same vein, hit the wall below. + #if Self.ypos >= Self.scrnheight-self.radius: - #self.yvelocity =-Self.yvelocity the #Bayi #if Self.ypos <= Self.radius: the #self.yvelocity = ABS (self.yvelocity) the #the upper and lower walls can also be replaced with the following two lines of code - ifSelf.ypos + Self.radius >= self.scrnheightorSelf.ypos-self.radius <=0: -Self.yvelocity =-self.yvelocity the the #Move a picture on the canvas the Self.canvas.move (Self.item, self.xvelocity, self.yvelocity) the - the classScreenSaver (): the " " the classes that define Screensavers94 can be started the " " the #a screen saver for randomly generated balls. theBalls = []98 About def __init__(self): - #the number of balls per start is random, within 6 to 50101Self.num_balls = Random.randint (6, 50)102 103Self.root =Tkinter. Tk ()104 #Cancel Border theSelf.root.overrideredirect (1)106 107 #any mouse movement needs to be canceled108Self.root.bind ('<Motion>', Self.myquit)109 the #get the size specifications of the screen111W, h =self.root.winfo_screenwidth (), Self.root.winfo_screenheight () the 113 #Create a canvas, including the ownership of the canvas, specifications theSelf.canvas = Tkinter. Canvas (Self.root, Width=w, height=h) the Self.canvas.pack () the 117 #draw a ball on the canvas118 forIinchRange (self.num_balls):119Ball = Randromball (Self.canvas, Scrnwidth=w, scrnheight=h) - Ball.create_ball ()121 self.balls.append (Ball)122 123 Self.run_screen_saver ()124 Self.root.mainloop () the 126 defRun_screen_saver (self):127 forBallinchSelf.balls: - Ball.move_ball ()129 the #after is a function that starts after 80 milliseconds, the function that needs to be started is the second argument131Self.canvas.after (80, Self.run_screen_saver) the 133 defMyquit (Self, event):134 Self.root.destroy ()135 136 if __name__=='__main__':137ScreenSaver ()
python-tkinter-Screensavers