2016-08-26 16 views
-2

私は、tkinterを使ってPythonで複数のフレームを持つ簡単なGUIを作ろうとしていますが、1つのフレームが他のフレームの上に表示されています。誰が私がなぜこれで、どのように修正できるのかを教えてくれますか?ここに私のコードは次のとおりです。 はフレームはtkinterのotheフレームの上に表示されます

This is a GUI built on Dylan5797's Scratch API 
''' 

import tkinter as tk 
from tkinter import ttk 

import scratchapi 

LARGE_FONT = ("Verdana", 12) 

class ScratchGUIApp(tk.Tk): 

    ''' 
    Main backend class, this is what makes stuff work. 
    ''' 

    def __init__(self, *args, **kwargs): 

     ''' Constructor ''' 

     # Call the parents constructor 
     tk.Tk.__init__(self, *args, **kwargs) 

     # Set the window title 
     tk.Tk.wm_title(self, "Scratch GUI") 

     # Create the container 
     self.container = tk.Frame(self) 
     self.container.pack(side="top", fill="both", expand=True) 

     # And configure the grid 
     self.container.grid_rowconfigure(0, weight=1) 
     self.container.grid_columnconfigure(0, weight=1) 

     # Create a dictionary of frames and append all pages to it 
     self.frames = {} 

     for f in (LoginPage, MainPage): 

      frame = f(self.container, self) 

      self.frames[f] = frame 

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

     # Set the starting page 
     self.show_frame(LoginPage) 

    def show_frame(self, cont): 

     # A simple function to switch pages 

     frame = self.frames[cont] 
     frame.tkraise() 

class LoginPage(tk.Frame): 

    ''' 
    This is all content on the login page 
    ''' 

    def __init__(self, parent, controller): 

     ''' Constructor ''' 

     # Call the parents constructor 
     tk.Frame.__init__(self, parent) 

     self.parent = parent 
     self.controller = controller 

     # Add the title 
     self.title = ttk.Label(self, text="Log in to your Scratch account", font=LARGE_FONT) 
     self.title.grid(row=0,column=0,columnspan=2,pady=10) 

     # Add the login form 
     self.usernameTag = ttk.Label(self, text="Username:") 
     self.usernameTag.grid(row=1,column=0,sticky="e",pady=2) 
     self.usernameEntry = ttk.Entry(self) 
     self.usernameEntry.grid(row=1,column=1,pady=2) 
     self.passwordTag = ttk.Label(self, text="Password:") 
     self.passwordTag.grid(row=2,column=0,sticky="e",pady=2) 
     self.passwordEntry = ttk.Entry(self) 
     self.passwordEntry.grid(row=2,column=1,pady=2) 

     # Just in case theres anything to report 
     self.errorMessage = ttk.Label(self, text="", foreground="red") 
     self.errorMessage.grid(row=3,column=0,columnspan=2,pady=5) 

     # Add the disclaimer 
     self.subtitle = ttk.Label(self, text="Account information is not collected in any way.") 
     self.subtitle.grid(row=4,column=0,columnspan=2) 

     # And finally add the login button 
     self.button = ttk.Button(self, text="Login", 
           command= lambda: self.login()) 
     self.button.grid(row=5,column=0,columnspan=2,pady=10) 

    def login(self): 

     # Attempts to log the user in to the scratchapi 

     usernameData = self.usernameEntry.get() 
     passwordData = self.passwordEntry.get() 

     if usernameData == "" or passwordData == "": 
      # Stop the function if the fields are empty. 
      self.errorMessage.config(text="These fields are required.") 
      return 

     # Attempt to login to the scratchapi with the given username and password 

     try: 
      scratch = scratchapi.ScratchUserSession(usernameData, passwordData) 
     except: 
      # Stop the function if there was an error 
      self.errorMessage.config(text="Login failed.") 
      return 

     self.controller.show_frame(MainPage) 

class MainPage(tk.Frame): 

    ''' 
    This is all content on the main page. 
    ''' 

    def __init__(self, parent, controller): 

     ''' Constructor ''' 

     # Call the parents constructor 
     tk.Frame.__init__(self, parent) 

     self.parent = parent 
     self.controller = controller 

     self.label=ttk.Label(text="hi") 
     self.label.pack(in_=self) 

app = ScratchGUIApp() 
app.mainloop() 
+0

コードが長すぎます。プロジェクト全体をコピー&ペーストする必要はありません。 [SO](http://stackoverflow.com)は、より具体的な質問のためのサイトです。 [MCVE](http://stackoverflow.com/help/mcve)を読んでください。また、あなた自身でこの問題を解決しようとしたことは私たちには示されていません。詳細は、[良い質問をする方法](http://stackoverflow.com/help/how-to-ask)を参照してください。 –

+0

しかし、ジオメトリ管理の問題のように見えます。このキーワードで研究を始めることをお勧めします。私を信頼すれば、あなたにとって有用な結果がたくさん得られます。 –

+0

スタックオーバーフローを初めて使用したことを謝罪します。 – Sigton

答えて

1

フレームが正確に表示されているSigtonことで SCRATCHのGUI を「「」どのようshould.The問題はあなたにもあるルートウィンドウで 『こんにちは』のラベルを、入れているということですあなたはフレームのためのコンテナを置く。 _inパラメータを使用しても、この特定のコードが(フレームを上げたり下げたりすることによって)動作するため、そのラベルの親はselfにする必要があります。

+0

ああ、私はLabelクラスを呼び出すときにパラメータとして 'self'を入れておくべきです。おっとっと。助けてくれてありがとう :) – Sigton

関連する問題