简体   繁体   English

Tkinter create_oval 方法不改变颜色

[英]Tkinter create_oval method does not change color

I've used buttons so when clicked they change the color of the ovals to obviously draw.我使用了按钮,因此当单击它们时,它们会更改椭圆的颜色以明显绘制。 The thing is no matter how I change the color variable it would always create a white oval.问题是无论我如何更改颜色变量,它总是会创建一个白色椭圆形。 Any help?有什么帮助吗?

Code:代码:

from tkinter import *

master = Tk()
master.title('Painting in Python')

canvas_width = 600
canvas_height = 450
color='white'
bg ='black'

def REDPEN():
    color='red'
    print(color)
def BLUEPEN():
    color='blue'
    print(color)
def GREENPEN():
    color='green'
    print(color)

def paint(event):
    x1,y1=(event.x-1),(event.y-1)
    x2,y2=(event.x+1),(event.y+1)
    c.create_oval(x1,y1,x2,y2,fill=color,outline=color,width=0)

RedButton = Button(master, text = "RED", command =REDPEN)
BlueButton = Button(master, text = "BLUE", command =BLUEPEN)
GreenButton = Button(master, text = "GREEN", command =GREENPEN)

RedButton.pack()
BlueButton.pack()
GreenButton.pack()


c=Canvas(master,width=canvas_width,height=canvas_height,bg=bg)

c.pack(expand=YES,fill=BOTH)
c.bind('<B1-Motion>',paint)

message=Label(master,text='Press and Drag to draw')
message.pack(side=BOTTOM)
master.mainloop()

When you change the value of the color variable in the functions, then the same is not reflected in the main color variable, this is the reason for your problem, since you never tell python that the variable color inside the function and outside the function are the same, so python considers the two to be different one local and the other global.当您在函数中更改颜色变量的值时,主颜色变量中没有反映相同的值,这就是您的问题的原因,因为您从未告诉 python function 内部和 ZC1C4252678E68385D1AAB5相同,因此 python 认为两者是不同的,一个是本地的,另一个是全局的。

The one easy fix for the same is to declare to python that you are referring to the global color variable whenever you use it in other functions using the global keyword of python this fixes your problem -:一个简单的解决方法是向 python 声明,当您使用全局关键字 python 在其他函数中使用全局颜色变量时,您指的是全局颜色变量,这解决了您的问题 - :

from tkinter import *

master = Tk()
master.title('Painting in Python')

canvas_width = 600
canvas_height = 450
color='white'
bg ='black'

def REDPEN():
    global color
    color='red'
    print(color)
def BLUEPEN():
    global color
    color='blue'
    print(color)
def GREENPEN():
    global color
    color='green'
    print(color)

def paint(event):
    global color
    x1,y1=(event.x-1),(event.y-1)
    x2,y2=(event.x+1),(event.y+1)
    c.create_oval(x1,y1,x2,y2,fill=color,outline=color,width=0)

RedButton = Button(master, text = "RED", command =REDPEN)
BlueButton = Button(master, text = "BLUE", command =BLUEPEN)
GreenButton = Button(master, text = "GREEN", command =GREENPEN)

RedButton.pack()
BlueButton.pack()
GreenButton.pack()


c=Canvas(master,width=canvas_width,height=canvas_height,bg=bg)

c.pack(expand=YES,fill=BOTH)
c.bind('<B1-Motion>',paint)

message=Label(master,text='Press and Drag to draw')
message.pack(side=BOTTOM)
master.mainloop()

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

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