バックグラウンド
こんにちは、みんな!私は現在、テキストファイルをロードおよび保存できる基本的なGUIテキストエディタに取り組んでいます。ここで学習したように、ツールバーとテキストボックスに複数のフレームを使用したいと思います。私はOOPを使用しており、メソッドにフレームを設定し、__init__メソッドにウィジェットを設定しましたwidget。何らかの理由で、ウィジェットをそれぞれのフレーム内に配置できません。
コード
from Tkinter import *
class Application:
    def __init__(self,parent):  #initialize the grid and widgets
        self.myParent = parent
        #Init the toolbar
        self.toolbar = Frame(parent)
        self.toolbar.grid(row = 0)
        #Init frame for the text box
        self.mainframe = Frame(parent)
        self.toolbar.grid(row = 1)
    def widget(self):#Place widgets here
        #Save Button
        self.saveButton = Button (self, self.toolbar,
                                  text = "Save", command = self.saveMe)
        self.saveButton.grid(column = 0, row = 0, sticky = W)
        #Open Button
        self.openButton = Button (self, self.toolbar,
                                 text = "Open", command = self.openMe)
        self.openButton.grid(column = 0, row = 1, sticky = W)
        #Area where you write 
        self.text = Text (self, self.mainframe,
                          width = (root.winfo_screenwidth() - 20),
                          height = (root.winfo_screenheight() - 10))
       self.text.grid(row = 2)
質問
- まださまざまな方法を使用していますが、各ウィジェットが正しいフレームに配置されていることを確認するにはどうすればよいですか? - これが不可能な場合は、OOPを使用してそれを行う方法を教えてください-私はその設定でTkinterに最も慣れており、改善することを約束しました。
 
- あなたの答えを説明してください。私は同族である必要があります-単にコンピューターに頭を頷いて、すぐに進むのではありません。 
- 追加のクレジット:OOPでTkinterを使用して複数のウィンドウ(各ウィンドウは異なるクラス)を初期化するにはどうすればよいですか?たとえば、これが私のコードだった場合: - class MainWindow(Frame): ---init stuff--- def widget(self): newWindow = Button(self, text = "click for a new window", command = self.window) newWindow.grid() def window(self): #What would I put in here to initialize the new window?? class theNextWindow(Frame):- ウィンドウを表示 - window.selfするためのメソッドには何を入れますか?- theNextWindow
みんなの助けてくれてありがとう!
編集1
self.widget()メソッドに行を追加する__init__と、この「素晴らしい」エラーが発生しました。
Traceback (most recent call last):
File "D:\Python Programs\Text Editor\MyTextv2.py", line 67, in <module>
 app = Application(root)
File "D:\Python Programs\Text Editor\MyTextv2.py", line 14, in __init__
 self.widget()
File "D:\Python Programs\Text Editor\MyTextv2.py", line 24, in widget
 text = "Save", command = self.saveMe)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2044, in __init__
 Widget.__init__(self, master, 'button', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
 BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
 self.tk = master.tk
AttributeError: Application instance has no attribute 'tk'
エラーログがここで私のメインループを明確に参照しているので、私File "D:\Python Programs\Text Editor\MyTextv2.py", line 67, in <module>
     app = Application(root)はそれを追加することにしました:
root = Tk()
root.title("My Text Editor")
#This is wierd - it gets the computer windows dimensions
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.overrideredirect(0)
#And then applies them here
root.geometry("%dx%d+0+0" % (w, h))
app = Application(root)
root.mainloop()