我是Tkinterer的新手,有人能帮我做一个基于矩阵的二维图形吗?

2022-04-05 00:00:00 python tkinter spyder

问题描述

我在网上尝试了所有的方法,但都不起作用。我只想要一个二维图,表示结果,这是一个矩阵。假设我的代码的这一部分是一个矩阵计算器,目前我在给出结果后弹出一个显示图形的屏幕时遇到了麻烦。顺便说一句,我没有错过任何图书馆。

 def inversa():
        a11=float(k1.get()) #saves user imput
        a12=float(k2.get())
        b21=float(k4.get())
        b22=float(k5.get())
        R1=[]
        R2=[]
        R1.append(a11)
        R1.append(a12)
        R2.append(b21)
        R2.append(b22)
        A =[R1,R2]
        A = np.matrix(A)
        mat2=np.linalg.inv(A)
        tkinter.messagebox.showinfo("The inverse matrix is : ","%s"%mat2)

解决方案

我建议查看Placing Plot on Tkinter Main Window in Python。您可以使用matplotlib首先绘制图形,然后将其放入tkinter窗口。

要将matplotlib图形集成到tkinter中,可以使用matplotlib的后端类,即FigureCanvis TkAgg。

导入-:

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

然后可以使用Figure对象将绘制的图形放置在图形画布上,如so-:

fig = Figure(...) # Initializing the figure object.
canvas = FigureCanvasTkAgg(fig, master=root) # Initializing the FigureCanvasTkAgg Class Object.
tk_canvas = canvas.get_tk_widget() # Getting the Figure canvas as a tkinter widget.
tk_canvas.pack() # Packing it into it's master window.
canvas.draw() # Drawing the canvas onto the screen.

还可以使用matplotlib方法绘制图形对象。

因此完整的代码变为-:

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg

fig = Figure(...) # Initializing the figure object.
canvas = FigureCanvasTkAgg(fig, master=root) # Initializing the FigureCanvasTkAgg Class Object.
tk_canvas = canvas.get_tk_widget() # Getting the Figure canvas as a tkinter widget.
tk_canvas.pack() # Packing it into it's master window.
canvas.draw() # Drawing the canvas onto the screen.

相关文章