我正在從一本書開始工作,並且有一個練習,用戶從單選按鈕中選擇一個圖形,並通過選擇一個複選按鈕來指定它是否被填充。在最初看起來像一個簡單的練習的日子裏奮鬥了幾天,我完全疲憊不堪。如何使用名爲「填充」的複選框來更改矩形和橢圓形的填充? 任何幫助表示讚賞。tkinter複選框選擇影響形狀
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
您的代碼示例格式不正確。 –
我重新粘貼它,使其看起來更好。這是你的意思嗎? – ohvonbraun
是的,它看起來好多了。 –