Tkinter create_oval 方法不改变颜色
Tkinter create_oval method does not change color
我使用了按钮,所以当点击它们时,它们会改变椭圆的颜色以明显绘制。问题是无论我如何更改颜色变量,它总是会创建一个白色椭圆形。有帮助吗?
代码:
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()
当您在函数中更改颜色变量的值时,相同的值不会反映在主颜色变量中,这就是您出现问题的原因,因为您从未告诉 python 该变量函数内部和函数外部的颜色相同,因此 python 认为两者不同,一个是局部的,另一个是全局的。
一个简单的解决方法是向 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()
我使用了按钮,所以当点击它们时,它们会改变椭圆的颜色以明显绘制。问题是无论我如何更改颜色变量,它总是会创建一个白色椭圆形。有帮助吗?
代码:
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()
当您在函数中更改颜色变量的值时,相同的值不会反映在主颜色变量中,这就是您出现问题的原因,因为您从未告诉 python 该变量函数内部和函数外部的颜色相同,因此 python 认为两者不同,一个是局部的,另一个是全局的。
一个简单的解决方法是向 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()