简体   繁体   中英

How to change the pen color in tkinter?

I'm using Tkinter to create a software that works like a sketchpad. One of the capabilities is for the user to be able to change the pen colour. I have no idea how to do so. How can the user change the pen colour?

tkinter doesn't have "pens"—so the whole concept does not apply. It does have something called a Canvas which defines a container object that can display stuff added ("drawn") on it (such as arcs, lines, textm etc). Here's some documentation about it. A Canvas instance contains other sub-objects to represent these sub-objects and each one of them has its own independent set of attributes that define things like its color, width, etc as applicable.

For example a Canvas line objects has a bunch of different ones which can be specified when instances of one are created. Here's a list of them . After a canvas object is created, some or all of its attributes can be changed by calling the universal configure() method on it.

One way to do this is to constantly create a tiny line wherever the mouse cursor is. Variables record where the cursor is, and when it is held down it draws. Here is an example of what you are trying to do (Python 3): https://gist.github.com/nikhilkumarsingh/85501ee2c3d8c0cfa9d1a27be5781f06

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM