【问题标题】:A Python tkinter smiley face with buttons that change gestures带有可改变手势的按钮的 Python tkinter 笑脸
【发布时间】:2019-04-14 04:16:17
【问题描述】:

更新: 我已经设法为我的代码和构造函数创建函数。我遇到的唯一困难是每当我运行我的程序时,当我相信我已经定义了它时,就会出现错误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()

【问题讨论】:

  • “提示和建议”与 stackoverflow 无关。您需要询问一个非常具体的问题,并解释您的代码如何产生与您想要的结果不同的结果。
  • 好的,感谢您的澄清。我只是想知道为什么我为四个手势定义的特定功能没有做任何事情。我在运行时生成的代码仅显示带有五个按钮的笑脸。点击按钮时不会改变当前的面部表情,例如,一旦我点击眨眼按钮,笑脸就不会改变。
  • 您应该edit 说出您在 cmets 中写的内容。这是我们可能能够帮助解决的具体问题。不过,如果您花时间将代码压缩为 minimal reproducible example,那将真的很有帮助。通常,这样做会帮助您自己解决问题。

标签: python canvas tkinter


【解决方案1】:

我遇到的唯一困难是每当我运行我的程序时, 有一个错误名称'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()

【讨论】:

    【解决方案2】:

    如果您已经创建了一个包含它们的类,则不需要为 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()
    

    总是有很多方法可以编写代码。对于什么是最好的方法,每个人都有不同的看法。最重要的是它有效并且您了解它为什么有效。

    【讨论】:

      猜你喜欢
      • 2018-05-17
      • 2011-04-23
      • 2013-11-10
      • 2013-04-19
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多