【发布时间】:2010-10-04 05:49:45
【问题描述】:
以下代码在我的 IDE (PyScripter) 中运行良好,但它不会在它之外运行。当我进入计算机然后 python26 并双击文件(在这种情况下为 .pyw)它无法运行。我不知道为什么会这样,任何人都可以解释一下吗?
顺便说一句,这是在 Windows 7 中。
我的代码:
#!/usr/bin/env python
import matplotlib
from mpl_toolkits.mplot3d import axes3d,Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
from matplotlib.ticker import LinearLocator, FixedLocator, FormatStrFormatter
import Tkinter
import sys
class E(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.protocol("WM_DELETE_WINDOW", self.dest)
self.main()
def main(self):
self.fig = plt.figure()
self.fig = plt.figure(figsize=(4,4))
ax = Axes3D(self.fig)
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
x = 10 * np.outer(np.cos(u), np.sin(v))
y = 10 * np.outer(np.sin(u), np.sin(v))
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v))
t = ax.plot_surface(x, y, z, rstride=4, cstride=4,color='lightgreen',linewidth=1)
self.frame = Tkinter.Frame(self)
self.frame.pack(padx=15,pady=15)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.frame)
self.canvas.get_tk_widget().pack(side='top', fill='both')
self.canvas._tkcanvas.pack(side='top', fill='both', expand=1)
self.btn = Tkinter.Button(self,text='button',command=self.alt)
self.btn.pack()
def alt (self):
print 9
def dest(self):
self.destroy()
sys.exit()
if __name__ == "__main__":
app = E(None)
app.title('Embedding in TK')
app.mainloop()
编辑:
我尝试在命令行中导入模块并收到以下警告。
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python26\lib\site-packages\matplotlib\__init__.py", line 129, in <module>
from rcsetup import defaultParams, validate_backend, validate_toolbar
File "C:\Python26\lib\site-packages\matplotlib\rcsetup.py", line 19, in <module>
from matplotlib.colors import is_color_like
File "C:\Python26\lib\site-packages\matplotlib\colors.py", line 54, in <module>
import matplotlib.cbook as cbook
File "C:\Python26\lib\site-packages\matplotlib\cbook.py", line 168, in <module>
class Scheduler(threading.Thread):
AttributeError: 'module' object has no attribute 'Thread'
>>>
编辑(2)
我尝试了 McSmooth 所说的并得到了以下输出。
Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import threading
>>> print threading.__file__
threading.pyc
>>> threading.Thread
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Thread'
>>>
【问题讨论】:
-
您是否看到错误消息,如果有,是什么?
-
请提供错误消息或说明它如何无法工作。
-
没有错误信息。就无法工作而言,它只是不会打开。没有窗口出现。
-
那么,你双击,什么都没有发生?多么奇怪。图标是否显示 python 标志,还是只是未知的文件类型图标?
-
是的,它显示了python标志。
标签: python windows-7 matplotlib