第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

tkinter GUI(燈光未顯示在我的窗口中)

tkinter GUI(燈光未顯示在我的窗口中)

慕田峪9158850 2024-01-04 16:29:19
三個燈通過單選按鈕操作誰能幫我找出原因嗎?下面的代碼沒有給出錯誤,但沒有輸出:( 在我的根目錄上出現(xiàn)其他圖標(biāo)(此處不包括),但下面的代碼部分不起作用?from tkinter import *root = Tk()frame = Frame(root)frame.pack()color = StringVar() radio_red = Radiobutton(frame, text="Red", bg="red", variable= color, value="R", command= on_RadioChange) radio_red.grid(row=10, column=1) radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable= color, value="Y", command= on_RadioChange)                radio_yellow.grid(row = 10, column = 2) radio_green = Radiobutton(frame, text="Green", bg="green", variable= color, value="G", command= on_RadioChange) radio_green.grid(row = 10, column = 3) canvas = Canvas(root, width=450, height=300, bg="white") canvas.pack() oval_red = canvas.create_oval(10, 10, 110, 110, fill="white") oval_yellow = canvas.create_oval(120, 10, 220, 110, fill="white") oval_green = canvas.create_oval(230, 10, 330, 110, fill="white")  color.set('R')  canvas.itemconfig(oval_red, fill="red")          root.mainloop()       def on_RadioChange():        color = color.get()        if color == 'R':            canvas.itemconfig(oval_red, fill="red")            canvas.itemconfig(oval_yellow, fill="white")            canvas.itemconfig(oval_green, fill="white")        elif color == 'Y':            canvas.itemconfig(oval_red, fill="white")            canvas.itemconfig(oval_yellow, fill="yellow")            canvas.itemconfig(oval_green, fill="white")        elif color == 'G':            canvas.itemconfig(oval_red, fill="white")            canvas.itemconfig(oval_yellow, fill="white")            canvas.itemconfig(oval_green, fill="green")
查看完整描述

1 回答

?
智慧大石

TA貢獻(xiàn)1946條經(jīng)驗 獲得超3個贊

您可能應(yīng)該了解如何使用類,但這里有一個有關(guān)如何在給定代碼中使用類的示例。


from tkinter import *


class TrafficLights:


    def value(self):


        root = Tk()


        frame = Frame(root)

        frame.pack()


        self.color = StringVar()


        radio_red = Radiobutton(frame, text="Red", bg="red", variable= self.color, value="R", command= self.on_RadioChange)

        radio_red.grid(row=10, column=1)


        radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable= self.color, value="Y", command= self.on_RadioChange)               

        radio_yellow.grid(row = 10, column = 2)


        radio_green = Radiobutton(frame, text="Green", bg="green", variable= self.color, value="G", command= self.on_RadioChange)

        radio_green.grid(row = 10, column = 3)


        self.canvas = Canvas(root, width=450, height=300, bg="white")

        self.canvas.pack()


        self.oval_red = self.canvas.create_oval(10, 10, 110, 110, fill="white")

        self.oval_yellow = self.canvas.create_oval(120, 10, 220, 110, fill="white")

        self.oval_green = self.canvas.create_oval(230, 10, 330, 110, fill="white")


        self.color.set('R')

        self.canvas.itemconfig(self.oval_red, fill="red")

        

        root.mainloop()


    def on_RadioChange(self):

        color = self.color.get()


        if color == 'R':

            self.canvas.itemconfig(self.oval_red, fill="red")

            self.canvas.itemconfig(self.oval_yellow, fill="white")

            self.canvas.itemconfig(self.oval_green, fill="white")

        elif color == 'Y':

            self.canvas.itemconfig(self.oval_red, fill="white")

            self.canvas.itemconfig(self.oval_yellow, fill="yellow")

            self.canvas.itemconfig(self.oval_green, fill="white")

        elif color == 'G':

            self.canvas.itemconfig(self.oval_red, fill="white")

            self.canvas.itemconfig(self.oval_yellow, fill="white")

            self.canvas.itemconfig(self.oval_green, fill="green")


a = TrafficLights()

a.value()

主要錯誤是您沒有使用self也沒有調(diào)用該方法value()。所以我修復(fù)了所有這些。因此,如果您不想調(diào)用value(),那么您可以將 Changedef value(self)與 def 一起使用__init__(self):,這樣您現(xiàn)在就不必調(diào)用了a.value()。我建議您在繼續(xù)學(xué)習(xí) python 和 tkinter 課程之前先學(xué)習(xí)更多有關(guān) OOP 的知識。


更新:


沒有什么太大的問題,只是重新排列代碼并將color內(nèi)部函數(shù)更改為colors其他內(nèi)容,完成的代碼將是:


from tkinter import *


root = Tk()


def on_RadioChange():

        colors = color.get()


        if colors == 'R':

            canvas.itemconfig('oval_red', fill="red")

            canvas.itemconfig('oval_yellow', fill="white")

            canvas.itemconfig('oval_green', fill="white")

        elif colors == 'Y':

            canvas.itemconfig('oval_red', fill="white")

            canvas.itemconfig('oval_yellow', fill="yellow")

            canvas.itemconfig('oval_green', fill="white")

        elif colors == 'G':

            canvas.itemconfig('oval_red', fill="white")

            canvas.itemconfig('oval_yellow', fill="white")

            canvas.itemconfig('oval_green', fill="green")


frame = Frame(root)

frame.pack()


color = StringVar()


radio_red = Radiobutton(frame, text="Red", bg="red", variable=color, value="R", command=on_RadioChange)

radio_red.grid(row=10, column=1)


radio_yellow = Radiobutton(frame, text="Yellow", bg="yellow", variable=color, value="Y", command=on_RadioChange)               

radio_yellow.grid(row = 10, column = 2)


radio_green = Radiobutton(frame, text="Green", bg="green", variable=color, value="G", command=on_RadioChange)

radio_green.grid(row = 10, column = 3)


canvas = Canvas(root, width=450, height=300, bg="white")

canvas.pack()


canvas.create_oval(10, 10, 110, 110, fill="white",tag='oval_red')

canvas.create_oval(120, 10, 220, 110, fill="white",tag='oval_yellow')

canvas.create_oval(230, 10, 330, 110, fill="white",tag='oval_green')


color.set('R')

canvas.itemconfig('oval_red', fill="red")

    

root.mainloop()

為什么color要改為colors?當(dāng)您使用color實際引用colorwith時color,這會引發(fā)一個UnboundLocalError: local variable 'color' referenced before assignment(因為它在函數(shù)內(nèi)部?),因此要擺脫它,只需重命名(僅在這種情況下)。這是最好的方法嗎?我不這么認(rèn)為,您可以在這里刪除一些無用的變量。


查看完整回答
反對 回復(fù) 2024-01-04
  • 1 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號