0

私は本から作業しており、ユーザーがラジオボタンから図を選択し、チェックボタンを選択してそれが塗りつぶされているかどうかを指定する演習を行っています。最初は単純なエクササイズのように思えたものに何日も苦労していると、私は完全に疲れ果ててしまいました. 「塗りつぶし」というチェックボックスを使用して、長方形と楕円形の塗りつぶしを変更するにはどうすればよいですか? どんな助けでも大歓迎です。

from tkinter import * 

class SelectShapes:
    def __init__(self):
        window = Tk() 
        window.title("Select Shapes") 

        self.canvas = Canvas(window, width = 500, height = 400, bg = "white" )
        self.canvas.pack()

        frame1 = Frame(window)
        frame1.pack()

        self.v1 = IntVar()
        btRectangle = Radiobutton(frame1, text = "Rectangle", variable = self.v1, value = 1, command = self.processRadiobutton)
        btOval = Radiobutton(frame1, text = "Oval", variable = self.v1, value = 2, command = self.processRadiobutton)
        btRectangle.grid(row = 2, column = 1)
        btOval.grid(row = 2, column = 2)

        self.v2 = IntVar()
        cbtFill = Checkbutton(frame1, text = "Fill", variable = self.v2, command = self.processCheckbutton)
        cbtFill.grid(row = 2, column = 3)


        window.mainloop()

    def processCheckbutton(self):
        if self.v2.get() == 1:
            self.v1["fill"] = "red"
        else:
            return False

    def processRadiobutton(self):
        if self.v1.get() == 1:
            self.canvas.delete("rect", "oval")
            self.canvas.create_rectangle(10, 10, 250, 200, tags = "rect")
            self.canvas.update()
        elif self.v1.get() == 2:
            self.canvas.delete("rect", "oval")
            self.canvas.create_oval(10, 10, 250, 200, tags = "oval")
            self.canvas.update()


SelectShapes()  # Create GUI 
4

1 に答える 1

0

問題はあなたのprocessCheckbutton機能にあります。何らかの方法self.v1でキャンバス オブジェクトとして考えているように見えますが、そうではありません。.. canvas オブジェクトの fill 属性を変更する行をそこに追加する必要があります。そのためには、まず現在のキャンバス オブジェクトの ID を保存する必要があります。IntVarCheckbutton

processRadioButton関数内:

        self.shapeID = self.canvas.create_rectangle(10, 10, 250, 200, tags = "rect")
#       ^^^^^^^^^^^^  save the ID of the object you create

        self.shapeID = self.canvas.create_oval(10, 10, 250, 200, tags = "oval")
#       ^^^^^^^^^^^^  save the ID of the object you create

そして最後にprocessCheckbutton関数で:

def processCheckbutton(self):
    if self.shapeID is not None:
        if self.v2.get() == 1:
            self.canvas.itemconfig(self.shapeID, fill="red")
#                                  ^^^^^^^^^^^^  use the saved ID to access and modify the canvas object.
        else:
            self.canvas.itemconfig(self.shapeID, fill="")
#                                                     ^^ Change the fill back to transparent if you uncheck the checkbox
于 2013-10-04T23:47:34.623 に答える