gpt4 book ai didi

Python:什么时候执行类->函数->括号。 & Matplotlib 图形不会显示

转载 作者:太空宇宙 更新时间:2023-11-03 18:52:58 25 4
gpt4 key购买 nike

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

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

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

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

有时一个有效,另一个无效?又名我的代码中的“事件”_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()

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

最佳答案

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

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

关于Python:什么时候执行类->函数->括号。 & Matplotlib 图形不会显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17883479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com