将脚本链接回Tkinter GUI Python

时间:2017-02-15 16:51:53

标签: python python-2.7 tkinter

我创建了一个Tkinter GUI,它从文本文件中获取数据,并根据一些用户输入绘制它。我从外部脚本调用函数并将绘图带回GUI时遇到问题。

我一般都是python的初学者,所以如果你发现任何糟糕的练习/不好的解释,请告诉我!

文件定义: GUI.py:包含GUI shell /小部件/按钮等 Plot.py:包含绘图算法

如果我将GUI和PLot算法结合在同一个脚本中,它可以正常工作,所以我不关心实际的算法/循环,只是当我把它分开时我才会这样做。虽然我试图利用google / stackexchange / youtube,但我还是不确定我是否完全明白这一点。

GUI.py(相关位):

class Application(Frame,object):
    def __init__(self, window):
        super(Application, self).__init__(window) #lets you invoke the method of a base class
        self.grid()
        self.create_widgets()
        self.window=window
    def create_widgets(self):
    #Button to analyse results
        self.process_bttn=Button(self,text="Process",command=self.Analyse)
        self.process_bttn.grid(row=m,rowspan=3,column=0,columnspan=5, sticky='nsew')   

    def Analyse(self):
        #When user hits 'process' button it starts this analyse, and plot routine
        self.Pro_txt.delete(0.0,END) #clears 'Processed' text box
        #definitions
        Input=self.load()
        DataType=self.update_data_type()
        ReadingOpt=self.update_readingopt()
        Blocksize=self.block_size()

        #Call 'Process' routine from external py
        Ans=Process(Input,DataType,ReadingOpt,Blocksize)

        if self.update_plotopt()=="Plot all on same axes\n":
            graph=Plotonsame(Ans)
        elif self.update_plotopt()=="Plot separately\n":
            graph=Plotseparately(Ans)

root=Tk() #creates the root window
root.title("Analysis") #sets title of window
root.geometry("1350x625") #sets size of window
app1=Application(root) #window1, corresponds to "self"

root.mainloop() #keeps window open (continuously loops)

" Plotonsame(Ans)"和" Plotseparately(Ans)"意味着通过" Ans"到我的外部" Plot.py" (我已导入)。

Plot.py:

def Plotonsame(s):
    #prepare canvas
    fig=figure.Figure(figsize=(8,6))            
    a=fig.add_subplot(111)

    for option in s:
        #s=Ans[option]
        self.Pro_txt.insert(END,s[option])
        #Plot the series
        a.plot(s[option], label=option)
        a.legend(bbox_to_anchor=(0,1.02,1,.102),loc=3,ncol=2,borderaxespad=0,prop={'size':10})
    app1.dataPlot=FigureCanvasTkAgg(fig,master=app1)
    app1.dataPlot.get_tk_widget().grid(row=1,rowspan=m,column=15,sticky="nsew")

def Plotseparately(s):
    s=self.Ans
    length=len(s) #count the number of options
    if length==1:
        j=1 #number of rows
        i=1 #number of columns
    if length==2:
        j=1
        i=2    
    elif length in (3,4):
        j=2
        i=2
    elif length in (5,6):
        j=2
        i=3
    elif length in (7,8,9):
        j=3
        i=3
    canvaswidth=9
    canvasheight=7
    index=0
    fig=figure.Figure(figsize=(canvaswidth,canvasheight))

    for option in s: #create subplots within the defined canvas 'fig'
        index+=1 #iterates over number of subplots
        a=fig.add_subplot(j,i,index) #adds the individual subplots
        self.Pro_txt.insert(END,s) #insert text(Ans[option]) into Processed window
        a.plot(self.Ans[option],label=option) #Plot the series
        a.legend(bbox_to_anchor=(0,1.02,1,.102),loc=3,ncol=2,prop={'size':7}) #adds the legend for each subplot
    dataPlot=FigureCanvasTkAgg(fig,master=self) #brings all plots together
    dataPlot.get_tk_widget().grid(row=1,rowspan=30,column=15,sticky="nsew")         #draws the plot frame with data etc

0 个答案:

没有答案