0
我正在使用tkinter編寫GUI,我希望窗口中的按鈕以某種方式更改眼睛和臉部。我無法定義我的功能來更改圓圈和線條。我想我可以自己撥打create_line
來更改畫布中的繪圖。在我定義w
之後,我嘗試將我的define
語句移動到我的程序底部,但沒有運氣。我得到的錯誤,如'App' object has no attribute 'create_line'
我是非常新的python,所以任何反饋將不勝感激。定義tkinter按鈕命令來更改幀中的畫布
# import tkinter
from tkinter import *
# define a new class
class App:
# define a command talk
def talk(self):
print("Talk button clicked...")
#w.self.create_line(45, 100, 90, 110)
# window of the GUI
def __init__(self, master):
# parent window
frame = Frame(master, bg = "#76F015")
# organizes the widgets into blocks
frame.pack()
# define a button in the parent window
self.button = Button(frame, text = "Talk", highlightbackground = "#D4D6D3",
fg = "black", command = self.talk)
self.button.pack(side = RIGHT)
# define a canvas
w = Canvas(frame, width = 125, height = 175, bg = "#76F015")
w.pack()
# draw a mouth
w.create_line(45, 100, 85, 100)
# run the main event loop
root = Tk()
app = App(root)
root.mainloop()
你需要說什麼樣的麻煩。你只用一個修改按鈕就可以更好地進行實驗。 REad https://stackoverflow.com/help/mcve –