我无法弄清楚如何在选中时复选框填充对象。我知道我可以使用颜色创建它们,但不知道如何使用复选框更改它们。
from tkinter import *
class box:
def __init__(self):
window = Tk()
window.title("Radiobuttons and Checkbuttons")
self.canvas = Canvas(window, width = 400, height = 100, bg = 'white')
self.canvas.pack()
frame = Frame(window)
frame.pack()
frame2 = Frame(window)
frame2.pack()
self.v1 = IntVar()
self.v2 = IntVar()
cbtFilled = Checkbutton(frame2,text= "Filled",variable = self.v2, command = self.processCheckButton).pack(side=RIGHT)
rbRect = Radiobutton(frame2, text = "Rectangle", variable = self.v1, value = 1, command = self.processRadiobutton).pack(side=LEFT)
rbOval = Radiobutton(frame2, text = "Oval", variable = self.v1, value = 2, command = self.processRadiobutton).pack(side = RIGHT)
def processCheckButton(self):
if self.v2.get() == 1:
print("Checked")
else:
print("unchecked")
def processRadiobutton(self):
if self.v1.get() == 1:
self.drawRectangle()
else:
self.drawOval()
def drawRectangle(self):
self.clearCanvas()
self.canvas.create_rectangle(35, 10 ,350,90,tags = 'rect')
def drawOval(self):
self.clearCanvas()
self.canvas.create_oval(50, 10, 350, 100, tags = 'oval')
def clearCanvas(self):
self.canvas.delete('rect','oval')
box()
答案 0 :(得分:0)
通过将形状分配给变量,我可以使用itemconfigure()方法对它们进行祭坛。
self.item=self.canvas.create_oval(50, 10, 350, 100, tags = 'oval')
self.item=self.canvas.create_rectangle(35, 10 ,350,90,tags = 'rect')
def processCheckButton(self):
if self.v2.get() == 1:
self.canvas.itemconfigure(self.item,fill='black')
else:
self.canvas.itemconfigure(self.item,fill='white')
答案 1 :(得分:0)
类框: def init (个体经营): window = Tk() window.title(" Radiobuttons和Checkbuttons") frame =框架(窗口) frame.pack() self.canvas = Canvas(frame,width = 400,height = 100,bg =' white') self.canvas.pack()
frame2 = Frame(window)
frame2.pack()
self.v1 = IntVar()
self.v2 = IntVar()
cbtFilled = Checkbutton(frame2,text= "Filled",variable = self.v2, command = self.processCheckButton).pack(side=RIGHT)
rbRect = Radiobutton(frame2, text = "Rectangle", variable = self.v1, value = 1, command = self.processRadiobutton).pack(side=LEFT)
rbOval = Radiobutton(frame2, text = "Oval", variable = self.v1, value = 2, command = self.processRadiobutton).pack(side = RIGHT)
def processCheckButton(self):
if self.v2.get() == 1:
self.clearCanvas()
if self.v1.get() == 1:
self.drawRectangle1()
elif self.v1.get()==2:
self.drawOval1()
# print("Checked")
else:
self.processRadiobutton()
#print("unchecked")
def processRadiobutton(self):
if self.v1.get() == 1:
self.drawRectangle()
else:
self.drawOval()
def drawRectangle(self):
self.clearCanvas()
self.canvas.create_rectangle(35, 10 ,350,90,tags = 'rect')
def drawOval(self):
self.clearCanvas()
self.canvas.create_oval(50, 10, 350, 100, tags = 'oval')
def clearCanvas(self):
self.canvas.delete('rect','oval')
def drawRectangle1(self):
self.clearCanvas()
self.canvas.create_rectangle(35, 10 ,350,90,fill = 'red',tags = 'rect')
def drawOval1(self):
self.clearCanvas()
self.canvas.create_oval(50, 10, 350, 100, fill= 'blue',tags = 'oval')
箱()