2016-07-20 11 views
0

私は、Windowsのサイズを変更させるためにcolumnconfigureまたはrowconfigureメソッドを使用しようとしたところにコメントがありますが、動作していないようです。私もmainloop()を取り出すことができ、私のプログラムはまったく同じように動作します。これは私がおそらく/ mainloopを正しく使用しているとは信じられません。メソッドは主に組織化のためのものであり、今は再利用可能なメソッドではないことがわかっています。私はたぶんそれをいくつかの点で修正する予定だったが、それはこの記事の目的ではない。ウィンドウのサイズを変更すると、これらのttkアプリケーションのサイズを強制的に変更するにはどうすればよいですか?

#!/usr/bin/python 

from tkinter import * 
from tkinter import ttk 
import webbrowser 
from dictionary import * 

class AI(): 

    def __init__(self): 
     self.urls = dictionary.get() 
     self.makeMainframe() 
     self.makeLabels() 
     self.makeDropDown() 
     self.makeButton() 
     self.makeEntry() 
     for child in self.mainframe.winfo_children(): 
      child.grid_configure(padx=2, pady=2) 
      #child.columnconfigure(index='all',weight=1) 
      #child.rowconfigure(index='all',weight=1) 

    def openMenu(self): 
     webbrowser.open(self.urls[self.lunchVar.get()]) 
     print(self.urls[self.lunchVar.get()]) 

    def saveChoice(self, event): 
     print(self.foodVar.get()) 

    def makeMainframe(self): 
     self.mainframe = ttk.Frame(padding="3 3 12 12") 
     self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S)) 
     #self.mainframe.columnconfigure('all', weight=1) 
     #self.mainframe.rowconfigure(index='all', weight=1) 

    def makeDropDown(self): 
     self.lunchVar = StringVar() 

     self.lunchChoice = ttk.Combobox(self.mainframe, textvariable=self.lunchVar, state='readonly') 
     self.lunchChoice['values'] = list(self.urls.keys()) 
     self.lunchChoice.grid(column=2, row=1, sticky=(W, E)) 

    def makeLabels(self): 
     ttk.Label(self.mainframe, text="Today's lunch is from...").grid(column=1, row=1, sticky=(E,W)) 
     ttk.Label(self.mainframe, text="Click here for a menu -->").grid(column=1, row=2, sticky=(E,W)) 
     ttk.Label(self.mainframe, text="What will you be having?").grid(column=1, row=3, sticky=(E,W)) 

    def makeButton(self): 
     self.menu = ttk.Button(self.mainframe, textvariable=self.lunchVar, command=self.openMenu) 
     self.menu.grid(column=2, row=2, sticky=(W, E)) 

    def makeEntry(self): 
     self.foodVar = StringVar() 
     self.foodChoice = ttk.Entry(self.mainframe, textvariable=self.foodVar) 
     self.foodChoice.grid(column=2, row=3, sticky=(E,W)) 
     self.foodChoice.bind("<Return>", self.saveChoice) 
     #self.foodChoice.columnconfigure(index='all', weight=1) 

AI=AI() 
mainloop() 
+1

インデントの一部が壊れています –

答えて

0

あなたはルートウィンドウでself.mainframeを置いていますが、ルートウィンドウ内の任意の行と列に重みを与えることはありません。また、明示的にルートウィンドウを作成することはありませんが、それは問題に寄与しません。また、"all"は有効な行または列ではありません。

メインフレームのサイズを変更してから、フレーム内のコンテンツに集中できます。

def makeMainframe(self): 
    ... 
    self.mainframe._root().columnconfigure(0, weight=1) 
    self.mainframe._root().rowconfigure(index=0, weight=1) 

注:あなたがに重みを与えることを覚えておく必要はありませんので、あなたが(たとえばルートウィンドウなど)いくつかの他のウィジェット内の単一のフレームを入れている場合には、packは間違いなくより良い選択であります行と列。あなたはこれを置き換えることができます。

self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S)) 
self.mainframe._root().columnconfigure(0, weight=1) 
self.mainframe._root().rowconfigure(index=0, weight=1) 

...これで:

self.mainframe.pack(fill="both", expand=True) 

をあなたはmainframeと同じことを行う必要があり、あなたが水平方向のスペースを埋めるために展開するエントリウィジェットをしたいと仮定すると:

def makeMainframe(self): 
    ... 
    self.mainframe.grid_columnconfigure(2, weight=1) 
+0

これは間違いなく機能しました。 pack()が何をしているのかを私に説明することができますか。明示的にルートウィンドウを作成することはありませんか?それは私がやるべきことですか?ありがとう、トン! – JustAGuy

+0

@JustAGuy:http://stackoverflow.com/a/3968033/7432。すべてのTkinter GUIにはルートウィンドウが必要です。ルートウィンドウは 'Tk()'のインスタンスで、明示的に作成しないと暗黙的に作成されます。 PEP8は明示的が暗黙的よりも優れていると言います。 http://effbot.org/tkinterbook/tkinter-hello-tkinter.htm –

+0

クラスの上に 'root = Tk()'のようなものを置くだけでいいですか?どこのコードであれ、どこで実装/使用する必要がありますか?もう一度おねがいします、申し訳ありません – JustAGuy

関連する問題