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 use Canvas Control in Tkinter programming

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly shows you "how to use Canvas controls in Tkinter programming". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn how to use Canvas controls in Tkinter programming.

In most cases, using controls can meet the needs of a simple program, but there are always scenarios where you need to draw something yourself, so you need to use the Canvas control. For example, the following go represents the program.

The following example illustrates the basic usage of the Canvas control.

Build the main window as you would with other controls:

# create the main windowroot = Tk ()

Determine the number and spacing of chessboard paths:

# config go panspace = 30pan_size = 13

The Canvas control is generated in a similar way to other controls, and width and height are used to specify the width and height of the control, respectively.

# create canvascanvas = Canvas (root, height= space * pan_size, width= space * pan_size) canvas.grid (row=0, column=0)

The following drawing actions are carried out through the Canvas control.

Generate and build brown rectangular chessboard. The parameters are the upper-left, lower-right coordinates, and fill color of.

# crate pancanvas.create_rectangle (space / 2, space / 2, space * pan_size-space / 2, space * pan_size-space / 2, fill ='# eeaa40')

Draw a checkerboard through a straight line object.

# draw horizental linesfor r in range (0, pan_size): canvas.create_line (space / 2, space / 2 + r * space, space * pan_size-space / 2, space / 2 + r * space) # draw vertical linesfor c in range (0, pan_size): canvas.create_line (space / 2 + c * space, space / 2, space / 2 + c * space, space * pan_size-space / 2)

The pawn drawing function uses oval objects and Text objects to depict sunspots and whites. If the number parameter is specified, it can also generate anti-white text on the pieces.

# color enum valueclass GoColor (Enum): WHITE = 0 BLACK = 1

# create fontftTimes = Font (family='Times', size=12)

# add godef set_go (row, col, color, number=0): r = 11 go_color = 'white' font_color =' black' if color==GoColor.BLACK: go_color = 'black' font_color =' white' # add go shape canvas.create_oval (space / 2 + col * space-r, space / 2 + row * space-r, space / 2 + col * space + r, space / 2 + row * space + r Fill=go_color) # add number if number > 0: canvas.create_text (space / 2 + col * space, space / 2 + row * space, font=ftTimes, fill=font_color, text=str (number))

The parameters specified by the code when generating the oval are the upper-left and lower-right coordinates, and the fill color; the location, font, and text are specified when generating the text object.

Finally, a number of chess pieces are added according to the small target formula.

# add gos.set_go (3,10, GoColor.BLACK) set_go (3,8, GoColor.WHITE) set_go (5,10, GoColor.BLACK, 1) set_go (2,10, GoColor.WHITE, 2) set_go (2,11, GoColor.BLACK, 3) set_go (2,9, GoColor.WHITE, 4) set_go (1,11, GoColor.BLACK, 5) set_go (2,4, GoColor.WHITE 6) these are all the contents of the article "how to use Canvas controls in Tkinter programming" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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

Internet Technology

Wechat

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

12
Report