【发布时间】:2018-03-12 16:47:41
【问题描述】:
所以我在这里有一个包含 2 个框架的基本 GUI。第一页有两个下拉菜单,用户可以在其中从数据框中选择列。它还包括一个按钮,当按下该按钮时,将两个选定的列都输出到标签上(这只是为了确保它选择正确)。
我试图让代码做的是,每次在下拉菜单中选择一个新列时,图表都会更新一个新的绘图。我认为这就像将这些分配给一个字符串变量一样简单,但这似乎不起作用。
还有一个带有图表的第二页,以防跨帧使用此功能更容易,但理想情况下它将保持在同一个页面上。
import pandas as pd
import tkinter as tk
#from tkinter import filedialog
from tkinter import ttk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
df = pd.DataFrame(
[[4, 5, 6],
[7, 8, 9],
[10, 11, 12]],
columns=['a', 'b', 'c'])
xList = []
yList = []
Options = df.dtypes.index
fig = Figure(figsize=(5,4), dpi=100)
ax= fig.add_subplot(111)
LARGE_FONT= ("Verdana", 12)
class GUI(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "GUI")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (Home, Graph):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Home)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Home(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10,padx=10)
def selectXY():
x = varX.get()
y = varY.get()
ax.clear()
X.config(text=x)
Y.config(text=y)
xList = df.loc[:,X]
yList = df.loc[:,Y]
return xList
return yList
#Y axis select
varY = tk.StringVar(self)
# initial value
varY.set('Select Y axis')
optionY = tk.OptionMenu(self, varY, *Options)
optionY.pack()
#X axis select
varX = tk.StringVar(self)
# initial value
varX.set('Select X axis')
optionX = tk.OptionMenu(self, varX, *Options)
optionX.pack()
button2 = tk.Button(self, text="Plot Axes", command = selectXY)
button2.pack()
button2 = ttk.Button(self, text="Graph",
command=lambda: controller.show_frame(Graph))
button2.pack()
Y = ttk.Label(self, text=yList)
Y.pack()
X = ttk.Label(self, text=xList)
X.pack()
df.plot.scatter(xList, yList, ax=ax)
canvas = FigureCanvasTkAgg(fig, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
class Graph(tk.Frame):
def __init__(self, parent, controller):
self.controller = controller
tk.Frame.__init__(self,parent)
label = tk.Label(self, text="Graph", font=LARGE_FONT)
label.pack(pady=10,padx=10)
df.plot.scatter(xList, yList, ax=ax)
canvas = FigureCanvasTkAgg(fig, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)
button3 = ttk.Button(self, text="Back",
command=lambda: controller.show_frame(Home))
button3.pack()
app = GUI()
app.mainloop()
【问题讨论】:
标签: python pandas user-interface tkinter graph