2016-12-29 53 views
1

將類定義tkinter對象放置在一個單獨的框架下面的建議,我從Sentdex我得到了一個多頁Python/Tkinter應用程序,其中包括提供一個時間移動圖生活在tk.Tk下的一套多框架的單幀。移動圖形的編碼稍微複雜一些,因此我選擇在類中定義畫布:GrpCanvas(tk.Canvas)。當許多定義使用tk.Tk

我的問題是,這個程序結構似乎會導致畫布對象出現在我的所有頁面框架上!我如何管理代碼,以便graphcanvas=GrpCanvas(HomePage)只出現在該頁面上?我已經評論了一些父母定義,以顯示我曾經嘗試過的(並失敗)。我正在使用Python 3.4.4。

我顯示下面的代碼(削減儘可能我可以顯示該問題):

#Avoiding canvas on all pages when pages are managed using tk.Tk 

import tkinter as tk 

sinewave_points=[] #Generated by a sin function of time. 
#class GrpCanvas(self, parent): 
class GrpCanvas(tk.Canvas):  
    #def __init__(self, parent): 
    def __init__(self, parent, controller):  
     tk.Canvas.__init__(self, height=340, width=594, bg='white')#,  x=pos_x, y=pos_y): 
     self.place(x=180, y=80) 

    def set_y_scale(self, sinewave_points): 
     self.scale=100 #actually calculated from a scaling algorithm (adapting to amplitude of sinewave_points) 
     return self.scale 

    def define_graph(self, scale, sinewave_points): 
     # create x-axis 
     self.horizontal=self.create_line(0, 170, 594, 170, width=2) 
     for i in range(13): #used to be 26 
      x = 20 + (i * 48) 
      self.x_scale=self.create_text(x, 175, font=("", 6),\ 
             anchor='n', text='{}'.format(((12/3) * i)-24)) 

     # y-axis 
     self.vertical=self.create_line(20, 330, 20, 10, width=2) 
     self.y_scale=self.set_y_scale(sinewave_points) 

     if self.y_scale == 100: 
      for i in range(21): 
       self.y = int(330 - (i * (320/20))) #In fact there is an slgorithm to scale the y-axis 
       #print(i, self.y) 
       self.y_axis=self.create_text(17, (self.y), font=("", 6), anchor='e',\ 
           text='{}'.format(int((((200/320)*(320/20)) * i)-\ 
           100))) 


     for i in range(len(sinewave_points)):      
      self.x, self.y = (i+20) , int(-1*self.scale*sinewave_points[i])+ 170 
      self.history=self.create_oval(self.x - 1, self.y - 1, self.x + 1,\ 
             self.y + 1, width=0, fill='purple') 



class Moving_Sinewave(tk.Tk): 
    def __init__(self, *args, **kwargs):   
     #Initialising Tkinter  

     tk.Tk.__init__(self, *args, **kwargs) 
     tk.Tk.wm_title(self, 'Sinewave Moving Plotter') 
     tk.Tk.geometry(self, '800x480')#This is the size of the screen (in pixels)  

     container = tk.Frame(self) 
     container.pack(fill='both', expand= True)#(side="top", fill="both", expand = True) 
     container.grid_rowconfigure (0, weight=1) 
     container.grid_columnconfigure(0, weight=1) 

     self.frames = {} 

     for F in (HomePage,     
       SystemConfigPage,     
       ConfigAlarmsPage): 

      frame = F(container, self) 
      self.frames[F] = frame 
      frame.grid(row=0, column=0, sticky="nsew") 
      frame.configure(background= 'ivory2'), 
     self.show_frame(HomePage) 

    def show_frame(self, cont):  
     frame=self.frames[cont] 
     frame.tkraise() 


class HomePage(tk.Frame): 

    def __init__(self, parent, controller): 
     self.controller=controller 
     tk.Frame.__init__(self, parent) 
     global time1, time2, time4, time5 

     sysconfigbutton=tk.Button(self, text= 'System\nConfiguration',   
           command=lambda: controller.show_frame(SystemConfigPage), 
           height=2, width=12) 
     sysconfigbutton.place(x=20, y=80) 

     #graphcanvas=GrpCanvas(tk.Frame)   #works with: class GrpCanvas(tk.Canvas):  
                  #def __init__(self, parent): 
     #graphcanvas=GrpCanvas(HomePage)   #works with: class GrpCanvas(tk.Canvas):  
                  #def __init__(self, parent): 
     #graphcanvas=GrpCanvas(HomePage(tk.Frame)) 

     graphcanvas=GrpCanvas(HomePage, controller.tk)# works with: class GrpCanvas(tk.Canvas):  
                #def __init__(self, parent, controller): 

     graphcanvas.define_graph(graphcanvas.set_y_scale(sinewave_points), sinewave_points) 
    # This actually plots the points 

class SystemConfigPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 

     configalarmsbutton=tk.Button(self, text= 'Configure\nAlarms', 
          command=lambda: controller.show_frame(ConfigAlarmsPage),         
          height=2, width=12) 
     configalarmsbutton.place(x=20, y=180) 

class ConfigAlarmsPage(tk.Frame): 

    def __init__(self, parent, controller): 
     tk.Frame.__init__(self, parent) 

     backbutton=tk.Button(self, text= 'Back', 
         command=lambda: controller.show_frame(HomePage), 
           height=2, width=12) 
     backbutton.place(x=20, y=380) 


app = Moving_Sinewave()    
app.mainloop() 

回答

0

的第一個參數GrpCanvas需要是父窗口部件,其中所述帆布是出現。在這種特殊情況下,你應該使用self,因爲你希望它是在主頁上,你正在創建它看成HomePage的一部分:

graphcanvas=GrpCanvas(self, controller.tk) 

您還需要在通過這個參數的的__init__父母課堂,你忽視了這一點。爲了解決這個問題,改變這種:

tk.Canvas.__init__(self, height=340, ...) 

...這樣的:

tk.Canvas.__init__(self, parent, height=340, ...) 

發生了什麼事是,因爲你沒有在父傳中,小部件用根窗口默認。而且由於您使用的是place,並且由於此小部件是最後創建的(因此位於堆疊順序的頂部),因此它出現在所有頁面的頂部。

+0

謝謝你,完美的作品。我看不到這一點。 – Oliver