简体   繁体   English

如何在Tkinter中更改笔的颜色?

[英]How to change the pen color in tkinter?

I'm using Tkinter to create a software that works like a sketchpad. 我正在使用Tkinter来创建像画板一样的软件。 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. tkinter没有“笔”,因此整个概念不适用。 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). 确实有一个称为Canvas东西,它定义了一个容器对象,可以显示在其上添加(“绘制”)的内容(例如弧,线,textm等)。 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. Canvas实例包含其他子对象来表示这些子对象,并且每个子对象都有其自己独立的属性集,这些属性定义了适用的颜色,宽度等内容。

For example a Canvas line objects has a bunch of different ones which can be specified when instances of one are created. 例如, Canvas 布线对象具有许多不同的对象,可以在创建一个对象的实例时指定它们。 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. 创建画布对象后,可以通过在其上调用通用configure()方法来更改其某些或所有属性。

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 这是您尝试执行的操作(Python 3)的示例: https : //gist.github.com/nikhilkumarsingh/85501ee2c3d8c0cfa9d1a27be5781f06

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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