用tkinter写一段代码实现“设置散点图等图像选择及显示按钮,选择相应图像后,点击,界面可显示对应图形”的功能
时间: 2024-02-15 08:03:36 浏览: 115
好的,以下是用 tkinter 实现“设置散点图等图像选择及显示按钮,选择相应图像后,点击,界面可显示对应图形”的代码:
```python
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
class ScatterPlot:
def __init__(self, master):
self.master = master
master.title("散点图")
self.fig = Figure(figsize=(5, 4), dpi=100)
self.ax = self.fig.add_subplot(111)
self.canvas = FigureCanvasTkAgg(self.fig, master=self.master)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.button_frame = tk.Frame(self.master)
self.button_frame.pack(side=tk.BOTTOM)
self.scatter_button = tk.Button(self.button_frame, text="散点图", command=self.plot_scatter)
self.scatter_button.pack(side=tk.LEFT)
self.line_button = tk.Button(self.button_frame, text="折线图", command=self.plot_line)
self.line_button.pack(side=tk.LEFT)
def plot_scatter(self):
self.ax.clear()
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
self.ax.scatter(x, y)
self.canvas.draw()
def plot_line(self):
self.ax.clear()
x = [1, 2, 3, 4, 5]
y = [1, 4, 9, 16, 25]
self.ax.plot(x, y)
self.canvas.draw()
root = tk.Tk()
scatter_plot = ScatterPlot(root)
root.mainloop()
```
这段代码实现了一个简单的界面,包括一个散点图和一个折线图的按钮。当用户点击“散点图”按钮时,界面会显示散点图;当用户点击“折线图”按钮时,界面会显示折线图。你可以根据自己的需要修改这段代码,添加更多图形类型和功能。
阅读全文