更新:
我已经设法为代码和构造函数创建函数。我遇到的唯一困难是,每当我运行程序时,当我相信自己已定义它时,就会出现错误name 'SmileyFace' is not defined
。关于为何未定义SmileyFace
的问题,我需要帮助。谢谢
from tkinter import *
class SmileyFace:
def __init__(self,righteye,mouth):
self.righteye=righteye
self.mouth=mouth
def smile(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = 'black')
c.delete(mouth)
mouth = c.create_arc(125, 225, 275, 275, start = 0, extent = -180, width = 5, fill = "white")
def sad(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_arc(125, 250, 275, 300, start = 0, extent = 180, width = 5, fill = "white")
def wink(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_line(225, 140, 250, 165, 275, 140, width = 5, smooth = "true")
c.delete(mouth)
mouth = c.create_line(125, 250, 275, 250, width = 5)
def grin(self):
global righteye, mouth
c.delete(righteye)
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(mouth)
mouth = c.create_line(125, 250, 200, 250, 275, 215, width = 5, smooth = "true")
def main():
global c
win = Tk()
c = Canvas(win, width = 800, height = 800)
c.pack()
c.create_oval(100, 100, 350, 350, outline = "black", fill = "yellow")
eye1 = c.create_oval(125, 125, 175, 175, fill = "black")
eye2 = c.create_oval(225, 125, 275, 175, fill = "black")
mouth = c.create_line(125, 250, 275, 250, width = 5)
Smiley = SmileyFace(righteye,mouth)
Button(win,text='Smile',command=Smiley.smile).pack
Button(win, text = "Sad", command = Smiley.sad).pack
Button(win, text = "Wink", command = Smiley.wink).pack
Button(win, text = "Grin", command = Smiley.grin).pack
Button(win, text = "Quit", command = win.destroy).pack
main()
答案 0 :(得分:0)
我遇到的唯一困难是每当我运行程序时, 错误名称“ SmileyFace”未定义...
当我尝试运行它时,解决了缩进问题后,我得到了NameError: name 'righteye' is not defined
。解决此问题后,由于最后没有win.mainloop()
,所以我运行它时什么也没有发生。解决此问题后,由于调用.pack
而不是.pack()
而看不到任何按钮。依此类推。
该程序的结构不佳。 SmileyFace
构造函数应该已经绘制了基本面并将右眼和嘴巴对象保存为属性,而不是尝试将它们作为全局变量来处理。下面,我采用一种更简单的方法来使用 tags 来避免属性和全局变量的共同使用:
from tkinter import *
class SmileyFace:
def __init__(self, canvas):
self.canvas = canvas
canvas.create_oval(70, 70, 350, 350, fill='yellow')
canvas.create_oval(125, 125, 175, 175, fill='black', tags='left')
canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def smile(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 225, 275, 275, extent=-180, width=5, fill='white', tags='mouth')
def sad(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_arc(125, 250, 275, 300, extent=180, width=5, fill='white', tags='mouth')
def wink(self):
self.canvas.delete('right||mouth')
self.canvas.create_line(225, 140, 250, 165, 275, 140, width=5, smooth='true', tags='right')
self.canvas.create_line(125, 250, 275, 250, width=5, tags='mouth')
def grin(self):
self.canvas.delete('right||mouth')
self.canvas.create_oval(225, 125, 275, 175, fill='black', tags='right')
self.canvas.create_line(125, 250, 200, 250, 275, 215, width=5, smooth='true', tags='mouth')
def main():
win = Tk()
canvas = Canvas(win, width=800, height=800)
canvas.pack()
smiley = SmileyFace(canvas)
Button(win, text='Smile', command=smiley.smile).pack()
Button(win, text='Sad', command=smiley.sad).pack()
Button(win, text='Wink', command=smiley.wink).pack()
Button(win, text='Grin', command=smiley.grin).pack()
Button(win, text='Quit', command=win.destroy).pack()
win.mainloop()
main()
答案 1 :(得分:0)
当您已经创建了包含它们的类时,就不必为 righteye 和 mouth 使用全局变量。使用类而不是全局变量。
您确实有多种格式错误。您必须注意Python中的空白。至关重要。
因此,重排空白,修复一些丢失的变量名,修复pack()
调用,并利用该类进行存储,我得到以下代码:
from tkinter import *
class SmileyFace:
def __init__(self, righteye, mouth):
self.righteye = righteye
self.mouth = mouth
def smile(self):
c.delete(self.righteye)
self.righteye = c.create_oval(225, 125, 275, 175, fill = 'black')
c.delete(self.mouth)
self.mouth = c.create_arc(125, 225, 275, 275, start = 0, extent = -180, width = 5, fill = "white")
def sad(self):
c.delete(self.righteye)
self.righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(self.mouth)
self.mouth = c.create_arc(125, 250, 275, 300, start = 0, extent = 180, width = 5, fill = "white")
def wink(self):
c.delete(self.righteye)
self.righteye = c.create_line(225, 140, 250, 165, 275, 140, width = 5, smooth = "true")
c.delete(self.mouth)
self.mouth = c.create_line(125, 250, 275, 250, width = 5)
def grin(self):
c.delete(self.righteye)
self.righteye = c.create_oval(225, 125, 275, 175, fill = "black")
c.delete(self.mouth)
self.mouth = c.create_line(125, 250, 200, 250, 275, 215, width = 5, smooth = "true")
if __name__ == '__main__':
win = Tk()
c = Canvas(win, width = 800, height = 800)
c.pack()
c.create_oval(80, 80, 350, 350, outline = "black", fill = "yellow")
eye1 = c.create_oval(125, 125, 175, 175, fill = "black")
righteye = c.create_oval(225, 125, 275, 175, fill = "black")
mouth = c.create_line(125, 250, 275, 250, width = 5)
Smiley = SmileyFace(righteye, mouth)
Button(win, text = 'Smile',command = Smiley.smile).pack()
Button(win, text = "sad", command = Smiley.sad).pack()
Button(win, text = "Wink", command = Smiley.wink).pack()
Button(win, text = "Grin", command = Smiley.grin).pack()
Button(win, text = "Quit", command = win.destroy).pack()
总是有很多编码方法。每个人对最佳方法都有不同的看法。最重要的是它起作用,并且您了解它为什么起作用。编码愉快!