tkinter - python Tkinter崩溃了每次运行但程序继续 Tkinter的最终抛光?

  显示原文与译文双语对照的内容
97 5

我是Tkinter新手和新手所以对我很轻松。我试图正确编码,但我必须做错误的事情。这些是我遇到的问题。

问题:

  • 当我移动 slider的时候,python 崩溃了,程序继续?随意
  • 缩放按钮不更新图形,但它的代码与滑块的代码相同?
  • 这个图周围有一个大的黑色板子,看起来很难看。
  • 代码的一般速度较差。

崩溃的( 继续运行) 为:

  • 运行时错误程序:C:Python27pythonw.exe [MS Visual C++ Runtime Library ] !
  • 在位置 0 x1e06016a [ tclnotifier的应用程序中发生异常未知软件异常( 0 x40000015 ):pythonw.exe ]

这就是我真正想知道的,只是为了解决这个问题,使它能够响应和友好。

感谢你的帮助

# ---------- Imports ------------------------------------- #
from Tkinter import *
import matplotlib
import numpy as np
# ---------- Settings ------------------------------------ #
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# ---------- Classes ------------------------------------ #
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._GUI_Design()
 self._GenData()
 self._Figure(self.Arm,self.Flag_Zoom)
 # Load the figure Window
 # Send to TK 
 self.canvas = FigureCanvasTkAgg(self.f, master=self.LF_Graph)
 def _GUI_Design(self):
 # Initial Frames 
 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=1, padx=5,pady=1)
 self.LF_Graph.grid(row=1, column=0,columnspan=2,padx=5,pady=3)
 # Insert Controls
 # Labels
 self.LB_Info = Label(self,text="Reduced Code", justify=LEFT)
 self.LB_SP = Label(self.LF_Control, text="Spoiler:") 
 # Slider
 self.SpScale = Scale(self.LF_Control, from_=0, to=120, orient=HORIZONTAL, length=300, resolution=1, command=self._UpdateCanvas, tickinterval=10) 
 # Buttons
 self.Zoomb = Button(self.LF_Control, text="Zoom", command=self._Zoom)
 self.Quit = Button(self.LF_Control, text="Quit", command=self.quit)
 # Grid Layout
 self.LB_Info.grid(row=0, column=0,padx=5,pady=1)
 self.LB_SP.grid(row=0, column=0,padx=10,pady=1)
 self.SpScale.grid(row=0, column=1,padx=10,pady=1)
 self.Zoomb.grid(row=0, column=3, padx=10,pady=1)
 self.Quit.grid(row=1, column=3, padx=10,pady=1)
 def _GenData(self):
 self.Arm = [[0,10,184.533],[0,153.734,164.932]] 
 def _UpdateCanvas(self, event):
 self._job = self.after(100, self._Execute(self.Flag_Zoom)) #miliseconds
 if self._job:
 self.after_cancel(self._job)
 def _Figure(self,Arm,Zoom):
 # Gen Figure
 self.f = plt.figure(1,figsize=(10,10))
 self.f.frame = False
 plt.clf()
 if Zoom == False:
 plt.axis([-500,2000,-1500,1000])
 else:
 plt.axis([-100,500,-100,500]) 
 plt.axes().set_aspect('equal', 'datalim')
 plt.plot(Arm[0],Arm[1],"g")
 def _Zoom(self):
 self._UpdateCanvas
 if self.Flag_Zoom == True:
 self.Flag_Zoom = False
 else:
 self.Flag_Zoom = True
 def _Execute(self,Zoom):
 RotA = self.RotateNO(self.Arm,self.SpScale.get(),[0,0])
 self._Figure(RotA,Zoom)
 self.canvas.draw()
 self.canvas.show()
 if self.canvas:
 self.canvas.get_tk_widget().grid(row=0, column=0)
# ---------- Calculations -------------------------------- #
def RotateNO(self,List,Angle,Orig): 
 # Angle: deg2rad
 th = -1.*float(Angle)/180*np.pi
 Rot = [[],[]]
 # Loop through
 for i in xrange(0,len(List[0])):
 X1 = (List[0][i]-Orig[0])*np.cos(th) - (List[1][i]-Orig[1])*np.sin(th)+Orig[0]
 Y1 = (List[0][i]-Orig[0])*np.sin(th) + (List[1][i]-Orig[1])*np.cos(th)+Orig[1]
 Rot[0].append(X1)
 Rot[1].append(Y1)
 return Rot 
# ---------- Execute Application ------------------------- #
root = Tk()
root.title("Rotation")
Exe = App(master=root)
Exe.mainloop()
root.destroy()
时间:原作者:1个回答

72 4

你需要RotateNO成为你的类的一部分。此外,缩放不需要采用 zoom 参数,因为你已经初始化了 self.Flag_Zoom也将 root.destroy() 更改为 Exe.destroy()尝试这个:

# ---------- Imports ------------------------------------- #
from Tkinter import *
import matplotlib
import numpy as np
# ---------- Settings ------------------------------------ #
matplotlib.use('TkAgg')
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
# ---------- Classes ------------------------------------ #
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._GUI_Design()
 self._GenData()
 self._Figure(self.Arm)
 # Load the figure Window
 # Send to TK 
 self.canvas = FigureCanvasTkAgg(self.f, master=self.LF_Graph)
 def _GUI_Design(self):
 # Initial Frames 
 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=1, padx=5,pady=1)
 self.LF_Graph.grid(row=1, column=0,columnspan=2,padx=5,pady=3)
 # Insert Controls
 # Labels
 self.LB_Info = Label(self,text="Reduced Code", justify=LEFT)
 self.LB_SP = Label(self.LF_Control, text="Spoiler:") 
 # Slider
 self.SpScale = Scale(self.LF_Control, from_=0, to=120, orient=HORIZONTAL, length=300, resolution=1, command=self._UpdateCanvas, tickinterval=10) 
 # Buttons
 self.Zoomb = Button(self.LF_Control, text="Zoom", command=self._Zoom)
 self.Quit = Button(self.LF_Control, text="Quit", command=self.quit)
 # Grid Layout
 self.LB_Info.grid(row=0, column=0,padx=5,pady=1)
 self.LB_SP.grid(row=0, column=0,padx=10,pady=1)
 self.SpScale.grid(row=0, column=1,padx=10,pady=1)
 self.Zoomb.grid(row=0, column=3, padx=10,pady=1)
 self.Quit.grid(row=1, column=3, padx=10,pady=1)
 def _GenData(self):
 self.Arm = [[0,10,184.533],[0,153.734,164.932]] 
 def _UpdateCanvas(self, event):
 self._job = self.after(100, self._Execute(self.Flag_Zoom)) #miliseconds
 if self._job:
 self.after_cancel(self._job)
 def _Figure(self,Arm):
 # Gen Figure
 self.f = plt.figure(1,figsize=(10,10))
 self.f.frame = False
 plt.clf()
 if not self.Flag_Zoom:
 print 'no zoom'
 plt.axis([-500,2000,-1500,1000])
 else:
 print 'zoom'
 plt.axis([-100,500,-100,500]) 
 plt.axes().set_aspect('equal', 'datalim')
 plt.plot(Arm[0],Arm[1],"g")
 def _Zoom(self):
 if self.Flag_Zoom:
 self.Flag_Zoom = False
 else:
 self.Flag_Zoom = True
 self._UpdateCanvas
 RotA = self.RotateNO(self.Arm,self.SpScale.get(),[0,0])
 self._Figure(RotA)
 self.canvas.draw()
 self.canvas.show()
 def RotateNO(self,List,Angle,Orig): 
 # Angle: deg2rad
 th = -1.*float(Angle)/180*np.pi
 Rot = [[],[]]
 # Loop through
 for i in xrange(0,len(List[0])):
 X1 = (List[0][i]-Orig[0])*np.cos(th) - (List[1][i]-Orig[1])*np.sin(th)+Orig[0]
 Y1 = (List[0][i]-Orig[0])*np.sin(th) + (List[1][i]-Orig[1])*np.cos(th)+Orig[1]
 Rot[0].append(X1)
 Rot[1].append(Y1)
 return Rot
 def _Execute(self,Zoom):
 RotA = self.RotateNO(self.Arm,self.SpScale.get(),[0,0])
 self._Figure(RotA)
 self.canvas.draw()
 self.canvas.show()
 if self.canvas:
 self.canvas.get_tk_widget().grid(row=0, column=0)
# ---------- Execute Application ------------------------- #
root = Tk()
root.title("Rotation")
Exe = App(master=root)
Exe.mainloop()
Exe.destroy()

要最小化 plot 中的灰色边框,请添加

plt.xticks([])
plt.yticks([])
plt.tight_layout(pad=0.0, w_pad=0.0, h_pad=0)

_Figure(self,Arm) 方法的末尾。这给了我:

tknoborder

( 我更改了 figsize,以便整个屏幕都适合我的屏幕)

原作者:
...