1

我被困住了。我试图从 Matplotlib 将一个数字编码到 Tk 中。我以前做过,但由于某种原因,这不起作用。

我也不明白何时使用“()”来调用函数,何时不使用:

#Example
def _Example(self)
    print "Blah blah"

# Do I call?
self._Example  # or
self._Example()

有时一个有效,一个无效??AKA 在我的代码 _UpdateCanvas 中的“事件”的情况下?

这会很有帮助,因为现在我似乎无法找出为什么我的身材没有加载?

# ----------------------- Import <<
from Tkinter import *
import matplotlib
import numpy as np
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# ----------------------- Class <<
class App(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self._job = None
        self.canvas = None
        self.Flag_Zoom = False
        self.pack()
        self._Data()        
        self._GUI_Design()

        # Create Canvas + Load Canvas
        self._Figure(self.Arm,self.Tri)
        self.canvas = FigureCanvasTkAgg(self.f,self.LF_Graph)
        self.canvas.draw()
        self.canvas.show()

    def _GUI_Design(self):
        # FRAME
        self.LF_Control = LabelFrame(self, text='Angle Controls', width=400, height=100 )
        self.LF_Graph = LabelFrame(self, text='Figure', width=400, height=400)
        self.LF_Control.grid(row=0, column=0, padx=5,pady=1)
        self.LF_Graph.grid(row=1, column=0,padx=5,pady=3)

        # LABELS
        self.LB_Info = Label(self.LF_Control,text="MOTION CONTACT EXAMPLE\nwith spoiler controls\n2013-07-23", justify=LEFT)
        self.LB_SP = Label(self.LF_Control, text="Spoiler:")
        self.LB_FP = Label(self.LF_Control, text="Triangle:")

        # SLIDERS
        self.SpScale = Scale(self.LF_Control, from_=0, to=45, orient=HORIZONTAL, length=350, resolution=1, command=self._UpdateCanvas, tickinterval=5)
        self.TrScale = Scale(self.LF_Control, from_=0, to=180, orient=HORIZONTAL, length=350, resolution=5, command=self._UpdateCanvas, tickinterval=30)

        # BUTTONS
        self.Bup = Button(self.LF_Control, text="  ^  \n  |  ", command=self._Bup)
        self.Bdn = Button(self.LF_Control, text="  |  \n  V  ", command=self._Bdn)
        self.Blf = Button(self.LF_Control, text=" <- "  , command=self._Blf)
        self.Brt = Button(self.LF_Control, text=" -> "  , command=self._Brt)

        # GRIDS
        self.LB_Info.grid(row=0, column=0,padx=1,pady=1,columnspan=2)
        self.LB_SP.grid(row=1, column=0,padx=1,pady=1)
        self.LB_FP.grid(row=2, column=0,padx=1,pady=1)
        self.SpScale.grid(row=1, column=1,padx=1,pady=1)
        self.TrScale.grid(row=2, column=1,padx=1,pady=1)
        self.Bup.grid(row=0, column=4 ,padx=1,pady=1)
        self.Bdn.grid(row=2, column=4 ,padx=1,pady=1)
        self.Blf.grid(row=1, column=3 ,padx=1,pady=1)
        self.Brt.grid(row=1, column=5 ,padx=1,pady=1)
        print "Graphics Loaded"

    def _Figure(self,Arm,Tri):
        self.f = plt.figure(1)      

        plt.axis([-500,500,-500,500])
        plt.axes().set_aspect('equal', 'datalim')
        plt.plot(Arm[0],Arm[1],"r")
        plt.plot(Tri[0],Tri[1],"g")
        print "figure set",Arm,Tri

    def _UpdateCanvas(self,event):
        print "def:_UpdateCanvas"
        self._job = self.after(100, self._Execute)  #miliseconds
        if self._job:
            self.after_cancel(self._job)

    def _Data(self):
        self.Arm  = [[0,100],[0,100]]
        self.Tri = [[50,150,100,50],[-50,-40,-120,-50]]

    def _Execute(self):
        print "def:_Execute"        
        self.SpAngle =  self.SpScale.get()
        self.TrAngle =  self.TrScale.get()

        self._Figure(self.Arm,self.Tri)
        self.canvas.draw()
        self.canvas.show()

    # Button Commands
    def _Bup(self):
        self.Tri[1] = [i+5 for i in self.Tri[1]]
        print "Button: UP"
        self._Execute()
    def _Bdn(self):
        self.Tri[1] = [i-5 for i in self.Tri[1]]
        print "Button: DOWN"        
        self._Execute()
    def _Blf(self):
        self.Tri[0] = [i-5 for i in self.Tri[0]]
        print "Button: LEFT"                
        self._Execute()
    def _Brt(self):
        self.Tri[0] = [i+5 for i in self.Tri[0]]
        print "Button: RIGHT"                
        self._Execute()      


# Execute App
root = Tk()
root.title("Contact")
Exe = App(master=root)
root.mainloop()

请我理解为什么没有数字加载?

4

1 回答 1

1

您总是使用括号来调用函数。如果您将函数用作command属性的参数,或者在绑定语句中使用它,则永远不要使用括号。

诀窍是知道何时要调用函数,以及何时要使用对该函数的引用。对于 Tkinter,command属性的值和语句的第二个参数bind必须始终是对稍后将调用的函数的引用。

于 2013-07-26T18:25:15.567 回答