我试图将导航工具栏放置在画布窗口中。但是,当我尝试运行代码时,只有工具栏出现。我尝试过的行也包含在#
下面。如何解决?
该代码是可执行的。第一个窗口的值已分配,只需单击“单击”按钮。
import numpy as np
import xlsxwriter
import tkinter as tk
from tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
window=Tk()
def open_window1():
y = 1/2
b = 1/4
result = []
u = []
v = []
t = []
p = [0,25,43.3013,50,43.3013,25,0,0,0,0,0,0]
a = []
pn = []
pn.append(0)
x = 0.0
for i in range(11):
z = x
t.append("%.1f" % z)
x = x + 0.1
m = 45594/100000
k = 18
c = 0.2865
u.append(0)
v.append(0)
a.append((p[0]-c*v[0]-k*u[0])/m)
dt = 0.1
a1 =(m/(b*dt*dt)+y*c/(b*dt))
a2 = (m/(b*dt)+(y/b-1)*c)
a3 = (((1/(2*b))-1)*m + dt*((y/(2*b))-1)*c)
kn = k + a1
for i in range(len(t)-1):
pn.append(np.around(p[i+1]+ a1*u[i] + a2*v[i] + a3*a[i], decimals=4))
u.append(np.around(pn[i+1]/kn, decimals=4))
v.append(np.around(y*(u[i+1]-u[i])/(b*dt) + (1-y/b)*v[i] + dt* (1-y/(2*b))*a[i], decimals=4))
a.append(np.around((u[i+1]-u[i])/(b*dt*dt) - v[i]/(b*dt)-(1/(2*b)-1)*a[i], decimals=4))
top = tk.Toplevel()
top.wm_geometry("1400x594")
top.title('Result table')
canvas = tk.Canvas(top,bg="white", width=1395, height=594)
canvas.grid(row=0,column=180)
#b1 = tk.Label(canvas, text="ti")
#b1.grid(row=3, column=0)
#b2 = tk.Label(canvas, text="Pi")
#b2.grid(row=3, column=200)
b1 = tk.Label(canvas, text="ti", font= "calibri 13", bg="white")
canvas.create_window(3,3, window=b1, anchor=tk.NW)
b2 = tk.Label(canvas, text="Pi", font= "calibri 13", bg="white")
canvas.create_window(150,3, window=b2, anchor=tk.NW)
b6 = tk.Label(canvas, text="Pni", font= "calibri 13", bg="white")
canvas.create_window(300,3, window=b6, anchor=tk.NW)
b3 = tk.Label(canvas, text="u''i", font= "calibri 13", bg="white")
canvas.create_window(450,3, window=b3, anchor=tk.NW)
b4 = tk.Label(canvas, text="u'i", font= "calibri 13", bg="white")
canvas.create_window(600,3, window=b4, anchor=tk.NW)
b5 = tk.Label(canvas, text="ui", font= "calibri 13", bg="white")
canvas.create_window(750,3, window=b5, anchor=tk.NW)
for i in range(1,len(t)):
b6 = tk.Label(canvas, text=t[i], font= "calibri 12", bg="white")
canvas.create_window(3,50*i, window=b6, anchor=tk.NW)
for i in range(1,len(t)):
b6 = tk.Label(canvas, text=p[i], font= "calibri 12", bg="white")
canvas.create_window(150,50*i, window=b6, anchor=tk.NW)
for i in range(1,len(t)):
b6 = tk.Label(canvas, text=pn[i], font= "calibri 12", bg="white")
canvas.create_window(300,50*i, window=b6, anchor=tk.NW)
for i in range(1,len(t)):
b6 = tk.Label(canvas, text=a[i], font= "calibri 12", bg="white")
canvas.create_window(450,50*i, window=b6, anchor=tk.NW)
for i in range(1,len(t)):
b6 = tk.Label(canvas, text=v[i], font= "calibri 12", bg="white")
canvas.create_window(600,50*i, window=b6, anchor=tk.NW)
for i in range(1,len(t)):
b6 = tk.Label(canvas, text=u[i], font= "calibri 12", bg="white")
canvas.create_window(750,50*i, window=b6, anchor=tk.NW)
f = Figure(figsize=(5,5), dpi=100)
q = f.add_subplot(111)
q.plot(t,a)
figure = FigureCanvasTkAgg(f, canvas)
figure.draw()
canvas.create_window(850, 3, window=figure.get_tk_widget(), anchor=tk.NW)
#toolbar= NavigationToolbar2Tk(figure, canvas)
#toolbar.update()
#canvas.create_window(850, 800, window=figure._tkcanvas, anchor=tk.NW)
b2=Button(window, text="Click", width=12, command=open_window1)
b2.grid(row=5,column=0)
window.title("Fill in the blanks")
window.geometry("300x150")
window.mainloop()
答案 0 :(得分:0)
调用NavigationToolbar2Tk(figure, canvas)
时,使用的基础方法是容器pack
上的canvas
,它与create_window
方法创建的其余小部件冲突。您需要创建一个Frame
来容纳工具栏,然后在框架上调用create_window
:
import tkinter as tk
import numpy as np
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
root = tk.Tk()
root.geometry("500x450")
fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))
canvas = tk.Canvas(root)
canvas.pack(fill="both",expand=True)
figure_canvas = FigureCanvasTkAgg(fig, root)
figure_canvas.draw()
toolbar_frame = tk.Frame(canvas)
toolbar = NavigationToolbar2Tk(figure_canvas, toolbar_frame)
toolbar.update()
canvas.create_window(0,0, window=figure_canvas.get_tk_widget(), anchor=tk.NW)
canvas.create_window(0,400, window=toolbar_frame, anchor=tk.NW)
root.mainloop()
但是从您当前的设置来看,create_window
的所有这些调用实际上并不需要。相反,您可以简单地使用grid
将它们排列起来,并创建一个额外的框架来存储toolbar
:
def open_window1():
y = 1/2
b = 1/4
...
top = tk.Toplevel()
top.wm_geometry("1400x594")
top.title('Result table')
canvas = tk.Canvas(top,bg="white", width=1395, height=594)
canvas.grid(row=0,column=180)
result_labels = ("ti","Pi","Pni","u''i", "u'i","ui")
for num, txt in enumerate(result_labels):
tk.Label(canvas, text=txt, font= "calibri 13", bg="white",width=10).grid(row=0,column=num,padx=12,pady=8)
for col, item in enumerate([t,p,pn,a,v,u]):
tk.Label(canvas, text=item[num], font="calibri 12", bg="white").grid(row=num+1,column=col,padx=12,pady=8)
f = Figure(figsize=(5,5), dpi=100)
q = f.add_subplot(111)
q.plot(t,a)
figure = FigureCanvasTkAgg(f, canvas)
figure.draw()
figure.get_tk_widget().grid(row=0,column=6,rowspan=7,padx=5,pady=2)
tool_bar_frame = Frame(canvas)
toolbar= NavigationToolbar2Tk(figure, tool_bar_frame)
toolbar.update()
tool_bar_frame.grid(row=7,column=6,pady=2)