import tkinter as tk import csv import pandas as pd import matplotlib.pyplot as plt from tkinter import filedialog from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg root = tk.Tk() root.title("数据科学基础") root.geometry("800x600") font = ("楷体", 16) root.option_add("*Font", font) root.configure(bg="pink") def import_csv_data(): global file_path, df, txt_data file_path = filedialog.askopenfilename() df = pd.read_csv(file_path) top_5 = df.head() txt_data.delete('1.0', tk.END) txt_data.insert(tk.END, top_5) label2 = tk.Label(root, text="请选择要显示的图像:") label2.pack(side="left") button1 = tk.Button(root, text="散点图") button1.pack(side="left") button2 = tk.Button(root, text="折线图") button2.pack(side="left") button3 = tk.Button(root, text="柱状图") button3.pack(side="left") fig_container = tk.Frame(root) fig_container.pack() def show_figure(): x = df.iloc[:, 0] y = df.iloc[:, 1] if plt.fignum_exists(1): plt.clf() # 清空画布 if button1["state"] == "normal": plt.scatter(x, y) elif button2["state"] == "normal": plt.plot(x, y) elif button3["state"] == "normal": plt.bar(x, y) canvas = FigureCanvasTkAgg(plt.gcf(), master=fig_container) canvas.draw() canvas.get_tk_widget().pack() button1.config(command=lambda: (button1.config(state="disabled"), button2.config(state="normal"), button3.config(state="normal"), show_figure())) button2.config(command=lambda: (button2.config(state="disabled"), button1.config(state="normal"), button3.config(state="normal"), show_figure())) button3.config(command=lambda: (button3.config(state="disabled"), button1.config(state="normal"), button2.config(state="normal"), show_figure())) btn_import = tk.Button(root,text="导入CSV文件",command=import_csv_data) btn_import.pack() txt_data = tk.Text(root) txt_data.pack() btn_show_figure = tk.Button(root, text="显示图像", command=lambda: (button1.config(state="normal"), button2.config(state="normal"), button3.config(state="normal"))) btn_show_figure.pack() root.mainloop()如何修改代码实现对界面右侧实现滑动上下拉动界面的功能
时间: 2024-02-21 17:56:47 浏览: 172
要实现界面右侧实现滑动上下拉动界面的功能,可以将图像框架 fig_container 放在一个滚动框架中,并将滚动框架的大小和位置与图像框架相同。可以使用 tkinter 中的 Scrollbar 和 Canvas 实现滚动框架。以下是修改后的代码:
```
import tkinter as tk
import csv
import pandas as pd
import matplotlib.pyplot as plt
from tkinter import filedialog
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
root = tk.Tk()
root.title("数据科学基础")
root.geometry("800x600")
font = ("楷体", 16)
root.option_add("*Font", font)
root.configure(bg="pink")
def import_csv_data():
global file_path, df, txt_data
file_path = filedialog.askopenfilename()
df = pd.read_csv(file_path)
top_5 = df.head()
txt_data.delete('1.0', tk.END)
txt_data.insert(tk.END, top_5)
label2 = tk.Label(root, text="请选择要显示的图像:")
label2.pack(side="left")
button1 = tk.Button(root, text="散点图")
button1.pack(side="left")
button2 = tk.Button(root, text="折线图")
button2.pack(side="left")
button3 = tk.Button(root, text="柱状图")
button3.pack(side="left")
fig_container = tk.Frame(root)
fig_container.pack(side="right", fill="both", expand=True)
# 添加滚动框架
canvas = tk.Canvas(fig_container)
canvas.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(fig_container, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# 将图像框架放入滚动框架中
frame = tk.Frame(canvas)
canvas.create_window((0, 0), window=frame, anchor="nw")
def show_figure():
x = df.iloc[:, 0]
y = df.iloc[:, 1]
if plt.fignum_exists(1):
plt.clf() # 清空画布
if button1["state"] == "normal":
plt.scatter(x, y)
elif button2["state"] == "normal":
plt.plot(x, y)
elif button3["state"] == "normal":
plt.bar(x, y)
# 将图像框架放在滚动框架中
canvas = FigureCanvasTkAgg(plt.gcf(), master=frame)
canvas.draw()
canvas.get_tk_widget().pack()
button1.config(command=lambda: (button1.config(state="disabled"), button2.config(state="normal"), button3.config(state="normal"), show_figure()))
button2.config(command=lambda: (button2.config(state="disabled"), button1.config(state="normal"), button3.config(state="normal"), show_figure()))
button3.config(command=lambda: (button3.config(state="disabled"), button1.config(state="normal"), button2.config(state="normal"), show_figure()))
btn_import = tk.Button(root,text="导入CSV文件",command=import_csv_data)
btn_import.pack()
txt_data = tk.Text(root)
txt_data.pack()
btn_show_figure = tk.Button(root, text="显示图像", command=lambda: (button1.config(state="normal"), button2.config(state="normal"), button3.config(state="normal")))
btn_show_figure.pack()
root.mainloop()
```
其中,滚动框架的大小和位置由以下代码控制:
```
fig_container = tk.Frame(root)
fig_container.pack(side="right", fill="both", expand=True)
# 添加滚动框架
canvas = tk.Canvas(fig_container)
canvas.pack(side="left", fill="both", expand=True)
scrollbar = tk.Scrollbar(fig_container, orient="vertical", command=canvas.yview)
scrollbar.pack(side="right", fill="y")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
# 将图像框架放入滚动框架中
frame = tk.Frame(canvas)
canvas.create_window((0, 0), window=frame, anchor="nw")
```
其中,fig_container 是图像框架所在的框架,canvas 是滚动框架,scrollbar 是滚动条。在 canvas 上添加滚动条和图像框架。通过 canvas.create_window() 将图像框架放入滚动框架中。通过 lambda 函数绑定 canvas 的 <Configure> 事件,使滚动框架可以根据图像框架的大小自动调整。
阅读全文