【问题标题】:Defining a tkinter button command to change a canvas in the frame [duplicate]定义 tkinter 按钮命令以更改框架中的画布 [重复]
【发布时间】: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()

【问题讨论】:

标签: python tkinter


【解决方案1】:

这是一个工作示例:

import tkinter as tk

class App(tk.Frame):
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, **kwargs)

        self.face = Face(self, width = 125, height = 175, bg = "#76F015")
        self.face.pack()

        btn = tk.Button(self, text="Smile", command=self.face.smile)
        btn.pack()

        btn = tk.Button(self, text="Normal", command=self.face.normal_mouth)
        btn.pack()

        btn = tk.Button(self, text="Quick smile", command=self.quick_smile)
        btn.pack()

    def quick_smile(self):
        self.face.smile()
        self.after(500, self.face.normal_mouth) # return to normal after .5 seconds

class Face(tk.Canvas):
    def __init__(self, master=None, **kwargs):
        tk.Canvas.__init__(self, master, **kwargs)

        # make outside circle
        self.create_oval(25, 40, 105, 120)

        # make eyes
        self.create_oval(40, 55, 60, 75)
        self.create_oval(70, 55, 90, 75)

        # make initial mouth
        self.mouth = [] #list of things in the mouth
        self.normal_mouth()

    def smile(self):
        self.clear(self.mouth) # clear off the previous mouth
        self.mouth = [
            self.create_arc(45, 80, 85, 100, start=180, extent=180)
            ]

    def normal_mouth(self):
        self.clear(self.mouth) # clear off the previous mouth
        self.mouth = [
            self.create_line(45, 100, 85, 100),
            self.create_arc(25, 95, 45, 105, extent=90, start=-45, style='arc'), # dimple
            self.create_arc(85, 95, 105, 105, extent=90, start=135, style='arc') # dimple
            ]

    def clear(self, items):
        '''delete all the items'''
        for item in items:
            self.delete(item)

def main():
    root = tk.Tk()
    win = App(root)
    win.pack()
    root.mainloop()

if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多