gpt4 book ai didi

python - 我将一个球体的 matplotlib 图嵌入到 Tkinter 中,但无法再围绕它运行!

转载 作者:太空狗 更新时间:2023-10-30 03:07:40 25 4
gpt4 key购买 nike

我将球体的 matplotlib 图嵌入到 Tkinter 中。现在由于某种原因,我在拖动鼠标时失去了绕物体旋转的能力。任何人都知道为什么会发生这种情况以及如何解决这个问题?

#!/usr/bin/env python
import matplotlib
matplotlib.use('TkAgg')


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=(3.5,3.5))
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=0)




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.toolbar = NavigationToolbar2TkAgg( self.canvas, self )
self.toolbar.update()
self.toolbar.pack()

self.btn = Tkinter.Button(self,text='button',command=self.alt)
self.btn.pack(ipadx=250)

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()

最佳答案

您需要在绘制之前设置您的 Canvas ,因此将下面的代码块移动到此行之后 self.canvas._tkcanvas.pack(side='top', fill='both', expand=1 )

        #Move this Code 
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=0)

关于python - 我将一个球体的 matplotlib 图嵌入到 Tkinter 中,但无法再围绕它运行!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3877411/

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