如何使用tkinter将条目输入matplotlib等高线图?

2024-06-28 06:13:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用matplotlib等高线图创建GUI,我使用tkinter的FigurecanvasTkagg函数将该图放在GUI上。但是我对tkinter是一个新手,我不知道如何改变等高线图使用的截面数量,也不知道如何改变tkinter的数量。我希望你能理解我的问题
在等高线功能中,plt.contour采用输入编号5,这会改变绘图中的截面数量。我希望能够使用GUI更改此数字。怎么可能呢?谢谢你的阅读

import tkinter as tk
from tkinter import *
from tkinter import filedialog as fd
import numpy as np
root = tk.Tk()
root.geometry("1920x1080")
root.state('zoomed')

def UploadAction():
    global filename
    filename = fd.askopenfilename()
    extension = pathlib.Path(filename).suffix
    contour_method()

def contour_method():
    rows, cols = na.shape
    x = np.arange(0, cols)
    y = np.arange(0, rows)
    x, y = np.meshgrid(x, y)

    #2D contour plot
    cont = plt.contourf(x, y, img, 5)
    plt.gca().invert_yaxis()

    fig = Figure(figsize=(15,8), dpi=80)
    # inv = figure_plot.gca().invert_yaxis()
    sub = fig.add_subplot(111)
    gra = sub.contourf(cont), sub.invert_yaxis()
    fig.colorbar(cont)
    canvas3 = FigureCanvasTkAgg(fig, master=frame3_title)
    canvas3.get_tk_widget().pack()
  button_1 = tk.Button(root, text='Import file', command=UploadAction)  
  button_1.pack()

root.mainloop()

Tags: import数量tkinterasnpfigguiplt
1条回答
网友
1楼 · 发布于 2024-06-28 06:13:35

我有我问题的答案。我就是这样解决的

首先,我创建了一个entry小部件,将entry.get()放在一个函数中,并将该函数链接到一个按钮。因此,当按下按钮时,用户输入将进入并传递到matplotlib等高线打印函数,并在画布中打印

my_entry = tk.Entry(frame3_title, width=100)
my_entry.grid(row=0, column=0, padx=5, pady=5)
def get_entry():
    global contents 
    contents = my_entry.get()
    contents = int(contents)    
    contour_sections() 

def contour_sections():
    # I made it global because I am accessing to delete this plot and re-plot    it    with press of button based on user input changes.
    global canvas4

    #canvas 3 is another plot which the current plot replaces upon user entry.
    canvas3.get_tk_widget().destroy()

    #Here na is my image array
    rows, cols = na.shape
    x1 = np.arange(0, cols)
    y1 = np.arange(0, rows)
    x1, y1 = np.meshgrid(x1, y1)

    #2D contour plot
    cont1 = plt.contourf(x1, y1, na, contents)
    plt.gca().invert_yaxis()

    fig1 = Figure(figsize=(15,8), dpi=80)
    # inv = figure_plot.gca().invert_yaxis()
    sub1 = fig1.add_subplot(111)
    gra = sub1.contourf(cont1), sub1.invert_yaxis()
    fig1.colorbar(cont1)

    canvas4 = FigureCanvasTkAgg(fig1, master=frame3_title)
    canvas4.get_tk_widget().grid(row=20, column=0, padx=5, pady=5)

#This is the function that resets entry widget and also destroys the canvas. So, that every time a user inputs a number for contour sections it wont create a plot over and over again and cause a memory leak.

def reset_contour_sec():
    canvas4.get_tk_widget().destroy()
    my_entry.delete(0, 'end') 

#button for getting the user input.
button_3 = tk.Button(frame3_title, text='Enter', command=get_entry)  
button_3.grid(row=0, column=10, padx=5, pady=5)

#Button to reset the entry and canvas.
button_4 = tk.Button(frame3_title, text='Re-enter', command= reset_contour_sec)  
button_4.grid(row=0, column=20,  padx=5, pady=5)

我希望这能为如何根据tkinter中的用户输入更改轮廓截面提供思路

相关问题 更多 >