2017-05-08 70 views
0

我正在做一個基於測驗的遊戲,我似乎無法弄清楚爲什麼我的小工具被放置在任何地方。我有我的StartPage都很好,但是當我在單獨的框架中添加頁面兩個小部件時,他們繼續使用所有不同的框架。Tkinter小部件將進入不同的框架?

import tkinter as tk 
from tkinter import * 

TITLE_FONT = ("Helvetica", 18, "bold") 


class foodtechquiz(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, PageThree, PageFour, PageFive): 

      page_name = F.__name__ 
      frame = F(parent=container, controller=self) 
      self.frames[page_name] = frame 


      frame.grid(row=0, column=0, sticky="nsew") 

     self.show_frame("StartPage") 

    def show_frame(self, page_name): 
     #Show a frame for the given page name 
     frame = self.frames[page_name] 
     frame.tkraise() 


class StartPage(tk.Frame): 

    def __init__(self, parent, controller): 

     tk.Frame.__init__(self, parent) 
     self.controller = controller 
     controller.geometry("600x500") 


     label = tk.Label(self, text="Food Technology Quiz", font=TITLE_FONT) 
     label.place(x=160, y=30) 

     name = tk.Label(self, text="By Jett Townsend", font=("comicsansms", 15), bg = "white") 
     name.place(x=220, y=410) 

     button1 = tk.Button(self, text="Health", 
         command=lambda: controller.show_frame("PageOne")) 
     button1.place(x=100, y=200) 
     button2 = tk.Button(self, text="Hygiene", 
         command=lambda: controller.show_frame("PageTwo")) 
     button2.place(x=275, y=200) 
     button3 = tk.Button(self, text="Equipment", 
         command=lambda: controller.show_frame("PageThree")) 
     button3.place(x=430, y=200) 
     button4 = tk.Button(self, text="Safety", 
         command=lambda: controller.show_frame("PageFour")) 
     button4.place(x=175, y=340) 
     button5 = tk.Button(self, text="Food Preperation", 
         command=lambda: controller.show_frame("PageFive")) 
     button5.place(x=350, y=340) 

     banana = PhotoImage(file = "C:/Users/Jett/Downloads/banana1.gif") 
     question_image = Label(self, image = banana) 
     question_image.photo = banana 
     question_image.place(x=70, y=85) 

     hands = PhotoImage(file = "C:/Users/Jett/Downloads/hygiene.gif") 
     question_image = Label(self, image = hands) 
     question_image.photo = hands 
     question_image.place(x=235, y=100) 

     frypan = PhotoImage(file = "C:/Users/Jett/Downloads/frypan.gif") 
     question_image = Label(self, image = frypan) 
     question_image.photo = frypan 
     question_image.place(x=400, y=100) 

     safety = PhotoImage(file = "C:/Users/Jett/Downloads/safety.gif") 
     question_image = Label(self, image = safety) 
     question_image.photo = safety 
     question_image.place(x=140, y=230) 

     foodprep = PhotoImage(file = "C:/Users/Jett/Downloads/foodprep1.gif") 
     question_image = Label(self, image = foodprep) 
     question_image.photo = foodprep 
     question_image.place(x=338, y=250) 



class PageOne(tk.Frame): 

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



class PageTwo(tk.Frame): 

    def __init__(self, parent, controller): 


     tk.Frame.__init__(self, parent) 
     self.controller = controller 

     hygieneheading = tk.Label(text="Hygiene", font=("comicsansms", 25), bg = "white") 
     hygieneheading.place(x=235, y=30) 
     question = tk.Label(text="1. Washing water with hot water kills more bacteria than washing with cold.", font=("arial", 10), bg = "white") 
     question.place(x=85, y=100) 
     truebutton = tk.Button(text="True") 
     truebutton.place(x=60, y=300) 
     truebutton.config(height=5, width = 32) 
     falsebutton = tk.Button(text="False") 
     falsebutton.place(x=315, y=300) 
     falsebutton.config(height=5, width = 32) 

class PageThree(tk.Frame): 

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

class PageFour(tk.Frame): 

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

class PageFive(tk.Frame): 

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


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

回答

1

在你PageTwo類,創建沒有確定它們的父窗口小部件。爲了確保您的小部件放置在正確的父項中,您應該將此父項指定爲小部件創建中的第一個參數。例如,在PageTwo的__init__()方法,寫:

truebutton = tk.Button(self, text="True") 

使truebuttonself一個孩子(這是法中表示PageTwo實例)。

Introduction to Tkinter

+1

謝謝你這麼多,正是我想要的。 – Jett71

+0

太棒了!如果解決了問題,您可以將答案標記爲已接受;) – Josselin

相關問題