【发布时间】:2017-04-13 20:38:08
【问题描述】:
我正在使用 tkinter 编写一个 GUI,我希望窗口中的按钮以某种方式改变眼睛和面部。我无法定义我的函数来更改圆圈和线条。我想我可以在自己上调用create_line 来更改画布中的绘图。在定义w 之后,我尝试将我的define 语句移动到程序的底部,但没有运气。我收到诸如'App' object has no attribute 'create_line' 之类的错误
我对 python 很陌生,所以任何反馈都将不胜感激。
# 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()
【问题讨论】:
-
你需要说什么麻烦。您的示例最好只使用一个修改器按钮进行试验。阅读stackoverflow.com/help/mcve