首页 文章

当使用tk.Tk定义多个时,将类定义的tkinter对象放在单个帧上

提问于
浏览
1

根据我从Sentdex获得的建议,我编写了一个多页Python / Tkinter应用程序,其中包括在tk.Tk下生活的许多帧的单个帧的单帧上提供时间移动图 . 移动图的编码稍微复杂,所以我选择在类中定义画布:GrpCanvas(tk.Canvas) .

我的问题是这个程序结构似乎导致画布对象出现在我的所有21个页面框架上!如何管理代码以便 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()

1 回答

  • 0

    GrpCanvas 的第一个参数需要是要在其中显示画布的父窗口小部件 . 在这种特定情况下,您应该使用 self ,因为您希望它位于主页中,并且您在创建 HomePage 时创建它:

    graphcanvas=GrpCanvas(self, controller.tk)
    

    您还需要将此参数传递给父类的 __init__ ,您忽略了这一点 . 要解决这个问题,请更改:

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

    ......对此:

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

    发生的事情是因为你没有传入父节点,所以小部件使用根窗口作为默认值 . 而且因为你正在使用 place ,并且因为这个小部件最后被创建(因此位于堆叠顺序的顶部)所以它出现在所有页面的顶部 .

相关问题