2017-05-08 2 views
0

Ich mache ein quizbasiertes Spiel und ich kann nicht herausfinden, warum meine Widgets überall platziert werden. Ich hatte meine StartPage alle gut, aber dann, als ich Seite zwei Widgets im seperaten Rahmen hinzufügte, gingen sie zu allen verschiedenen Rahmen weiter.Tkinter Widgets gehen auf verschiedene Frames?

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() 

Antwort

1

In Ihrem PageTwo Klasse erstellen Sie Widgets ohne ihre Eltern zu definieren. Um sicherzustellen, dass sich Ihre Widgets im richtigen übergeordneten Element befinden, sollten Sie dieses übergeordnete Element als erstes Argument bei der Widget-Erstellung angeben. Zum Beispiel in __init__() Methode des PageTwo, schreiben:

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

truebutton ein Kind self zu machen (die in diesem Verfahren die PageTwo Instanz darstellt).

Introduction to Tkinter

+1

thankyou genau so viel, was ich wollte. – Jett71

+0

Großartig! Sie können die Antwort als akzeptiert markieren, wenn es Ihr Problem behoben hat;) – Josselin

Verwandte Themen