Python Tkinter多个框架与背景图像

时间:2015-03-25 07:39:18

标签: python tkinter

我是Tkinter的新手,我正在为我的RasPi挣扎一些相当简单的python代码。我的目标是拥有一系列具有相同固定大小和背景图像但具有不同按钮,标签(在图像顶部)和功能的框架。如果我在容器中包含一个画布和一个图像,那么我可以得到我的帧(至少是第一帧)以显示背景图像,但是我不能从特定于页面的类中向画布添加任何内容,因为我可以& #39; t再引用画布?如果我将框架留在容器中,然后在特定于页面的类中添加画布/图像,那么我就无法使画布/图像生效。这是我试图修改以满足我的需求的代码......

import Tkinter as tk

TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

    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 (StartPage, PageOne, PageTwo):
        frame = F(container, self)
        self.frames[F] = frame
        frame.grid(row=0, column=0, sticky="nsew")

    self.show_frame(StartPage)

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

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="This is the start page",                    font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One", 
                        command=lambda: controller.show_frame(PageOne))
        button2 = tk.Button(self, text="Go to Page Two",
                        command=lambda: controller.show_frame(PageTwo))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent) 
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page", 
                       command=lambda: controller.show_frame(StartPage))
        button.pack()

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page", 
                       command=lambda: controller.show_frame(StartPage))
        button.pack()

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()controller.show_frame(PageTwo))

1 个答案:

答案 0 :(得分:0)

我开始工作了......

import Tkinter as tk
import ttk

TITLE_FONT = ("Helvetica", 18, "bold")
class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        self.attributes("-fullscreen", True)
        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 (StartPage, PageOne, PageTwo):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

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

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        logo = tk.PhotoImage(file="/home/pi/Saffi.gif")
        BGlabel = tk.Label(self,image=logo)
        BGlabel.image = logo
        BGlabel.place(x=0,y=0,width=592,height=450)
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.place(x=0,y=0,width=592,height=44)

        button1 = tk.Button(self, text="Go to Page One", 
                        command=lambda: controller.show_frame(PageOne))
        button2 = tk.Button(self, text="Go to Page two", 
                        command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Exit",
                        command=self.quit)
        button1.place(x=100,y=406,width=200,height=44)
        button2.place(x=300,y=406,width=200,height=44)
        button3.place(x=500,y=406,width=80,height=44)


class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        logo = tk.PhotoImage(file="/home/pi/Saffi.gif")
        BGlabel = tk.Label(self,image=logo)
        BGlabel.image = logo
        BGlabel.place(x=0,y=0,width=592,height=450)
        label = tk.Label(self, text="This is page one", font=TITLE_FONT)
        label.place(x=0,y=0,width=592,height=44)

        button1 = tk.Button(self, text="Go to Start Page", 
                        command=lambda: controller.show_frame(StartPage))
    #button2 = tk.Button(self, text="Go to Page two", 
     #                   command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Exit",
                        command=self.quit)
        button1.place(x=100,y=406,width=200,height=44)
        button3.place(x=300,y=406,width=200,height=44)

class PageTwo(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        logo = tk.PhotoImage(file="/home/pi/Saffi.gif")
        BGlabel = tk.Label(self,image=logo)
        BGlabel.image = logo
        BGlabel.place(x=0,y=0,width=592,height=450)
        label = tk.Label(self, text="This is page two", font=TITLE_FONT)
        label.place(x=0,y=0,width=592,height=44)

        button1 = tk.Button(self, text="Go to Start Page", 
                        command=lambda: controller.show_frame(StartPage))
    #button2 = tk.Button(self, text="Go to Page two", 
     #                   command=lambda: controller.show_frame(PageTwo))
        button3 = tk.Button(self, text="Exit",
                        command=self.quit)
        button1.place(x=100,y=406,width=200,height=44)
        button3.place(x=300,y=406,width=200,height=44)

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()