问题描述
我对 Python 或一般编码相当陌生。我试图找到我的问题的解决方案,但经过数小时的谷歌搜索后,我仍然找不到我的问题的解决方案。因此,我希望这里有人可以帮助我。此外,我不是在寻找解决问题的最聪明的方法,而是寻找对我这样的新手来说易于理解的解决方案。
所以,我的问题: 我已经有一些代码来计算不同的东西。这一切正常。我现在想用 tkinter 创建一个相当高级的 GUI,我可以在其中实现我的计算等。 GUI 应该有几个页面,所有页面都有不同的计算选项和一个图形。 我在页面中使用了 Bryan Oakley(在论坛中找到)的示例。这一切正常。我的问题是如何创建仅出现在每个单独页面上的图表。到目前为止,我的做法是,图表始终显示在主框架中,位于各个页面的上方。同样以这种方式,程序卡在某种循环中,因此尽管我关闭了主窗口,但代码仍在运行。 希望你明白我想问什么^^
请在下面找到我正在使用的(缩短)代码。它很长,但正如我所说,我对这些东西很陌生,我不知道如何更准确地描述我的问题。
import tkinter as tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
class Page(tk.Frame):
def __init__(self,*args,**kwargs):
tk.Frame.__init__(self,**kwargs)
def show(self):
self.lift()
class Page1(Page):
def __init__(self,**kwargs):
global var1,var2,var3,var4
Page.__init__(self,**kwargs)
self.configure(bg="lightblue")
self.B_load = tk.Button(self,text = "load data",fg = "black",height = 2,width = 20).place(x=10,y=10)
var1 = tk.IntVar()
self.c1 = tk.Checkbutton(self,text='Show all samples (var1)',bg='lightblue',variable=var1,onvalue=1,offvalue=0,height=1,width=22,anchor='w',command=Var1).place(x=10,y=260)
Application()
class Page2(Page):
def __init__(self,**kwargs):
Page.__init__(self,**kwargs)
class MainView(tk.Frame):
def __init__(self,**kwargs):
tk.Frame.__init__(self,**kwargs)
#self.configure(bg="black")
p1 = Page1(self)
p2 = Page2(self)
buttonframe = tk.Frame(self)
container = tk.Frame(self)
buttonframe.pack(side="top",fill="x",expand=False)
container.pack(side="top",fill="both",expand=True)
p1.place(in_=container,x=0,y=0,relwidth=1,relheight=1)
p2.place(in_=container,relheight=1)
b1 = tk.Button(buttonframe,text="Main",command=p1.lift)
b2 = tk.Button(buttonframe,text="Basic Rock-Eval",command=p2.lift)
b1.pack(side="left")
b2.pack(side="left")
p1.show()
def Var1():
global var1
print("Var1=",var1.get())
class Application(tk.Frame):
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.createWidgets()
def createWidgets(self):
fig,ax1 = plt.subplots(figsize=(8,4))
canvas=FigureCanvasTkAgg(fig,master=root)
canvas.get_tk_widget().place(x=250,y=50)
canvas.draw()
def plot(self):
ax1.clear()
FID= [1,2,3,4]
T= [1,4]
ax1.set(xlim=(0,len(FID)),ylim=(0,max(FID)*1.1))
ax2.set(ylim=(0,max(T)+50))
ax1.plot(y_1,x,'grey')
ax2.plot(y_2,'r')
canvas.draw()
root = tk.Tk()
main = MainView(root)
main.pack(side="top",expand=True)
root.wm_geometry("1150x700")
root.mainloop()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)