Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to draw with Python tkinter Library

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python tkinter library drawing", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to use Python tkinter library drawing" bar!

First, the drawing of small houses

Example code:

# coding=utf-8import tkinter as tk # Import tkinter module root = tk.Tk () # create a top-level window root.title ('small house 1') # set title canvas = tk.Canvas (root, bg='white', width=700, height=700) # create canvas canvas on the root window with a white background The width and height are both 700 pixels canvas.pack (anchor='center') # canvas centered on the root display points = [(50,250), (350,50), (650,250)] # Triangle vertex coordinates canvas.create_polygon (points, fill='gray', outline='black', width=10) # White fill, red lines Line width 10canvas.create_rectangle ((200,250,500,550), fill='white', outline='black', width=10) # draw rectangle, white fill, green line, line width 10canvas.create_oval ((250,300,450,500), fill='purple', outline='black', width=10) # draw circle, yellow fill, yellow line The linewidth is 10 root.mainloop () # to enter the message loop

Running result:

Second, color bubble animation drawing

Example code:

# coding=utf-8import tkinter as tkimport random as rdimport time# global variables, all list objects # are: X direction speed, y direction speed, radius, position, graphic marks speedXList, speedYList, rList, posList, idList = [], [] # optional color colorList = ['pink',' gold', 'lightblue',' lightgreen', 'silver'] # canvas width and height And the number of graphics width, height, num = 400,400, 5root = tk.Tk () # create and layout canvas canvas = tk.Canvas (root, width=width, height=height, background='white') canvas.pack () for i in range (num): # randomly generate the initial position of the graph x = rd.randint (100,100) y = rd.randint (100,height-100) # add to the drawing location list posList.append ((x) Y)) # Random generated radius And add to the radius list r = rd.randint (20,50) rList.append (r) # randomly select a color color = rd.sample (colorList, 1) # to create an ellipse / circle Fill id with selected colors id = canvas.create_oval (x-r, y-r, x + r, y + r, fill=color, outline=color) # Save graphic identity idList.append (id) # set random movement speed And save speedXList.append (rd.randint (- 10,10)) speedYList.append (rd.randint (- 10,10)) while True: for i in range (num): # the current location of the graph item = posList [I] r = rList [I] # if the x position exceeds the boundary, adapt the x velocity direction if item [0]-r

< 0 or item[0] + r >

Width: speedXList [I] =-speedXList [I] # if the y position exceeds the boundary, adapt the y velocity direction if item [1]-r

< 0 or item[1] + r >

Height: speedYList [I] =-speedYList [I] # calculate the new position posList [I] = (item [0] + speedXList [I], item [1] + speedYList [I]) x, y = posList [I] [0], posList [I] [1] # move to the new position canvas.coords (idList [I], (x-r, y-r, x + r) Y + r)) # refresh screen canvas.update () # wait 0.1s That is, 10 frames per second are updated to form an animated time.sleep.

Running result:

III. Canvas creation

Example code:

Import tkinter as tk # Import the tkinter library and rename it tkmywindow = tk.Tk () # create a form mywindow.title ("I am a canvas") # set the title of the form mycanvas = tk.Canvas (mywindow, width=400, height=300, bg= "purple") # create the canvas and layout mycanvas.pack () mywindow.mainloop () # display the canvas

Running result:

Thank you for your reading, the above is the content of "how to use Python tkinter library drawing", after the study of this article, I believe you have a deeper understanding of how to use Python tkinter library drawing, the specific use of the situation also needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report