2012-05-05 18 views
6

私はPythonでTkinterを使って簡単なUIを作成しようとしています。グリッド内でウィジェットをサイズ変更することはできません。メインウィンドウのサイズを変更すると、入力ウィジェットとボタンウィジェットはまったく調整されません。Tkグリッドのサイズが正しく調整されない

class Application(Frame): 
    def __init__(self, master=None): 
     Frame.__init__(self, master, padding=(3,3,12,12)) 
     self.grid(sticky=N+W+E+S) 
     self.createWidgets() 

    def createWidgets(self): 
     self.dataFileName = StringVar() 
     self.fileEntry = Entry(self, textvariable=self.dataFileName) 
     self.fileEntry.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W) 
     self.loadFileButton = Button(self, text="Load Data", command=self.loadDataClicked) 
     self.loadFileButton.grid(row=0, column=3, sticky=N+S+E+W) 

     self.columnconfigure(0, weight=1) 
     self.columnconfigure(1, weight=1) 
     self.columnconfigure(2, weight=1) 

app = Application() 
app.master.title("Sample Application") 
app.mainloop() 

答えて

14

ルートウィンドウを追加し、フレームウィジェットも展開するように設定します。それは問題です。もし指定していなければ暗黙的なルートウィンドウがあり、フレーム自体は適切に拡張されていないものです。

root = Tk() 
root.columnconfigure(0, weight=1) 
app = Application(root) 
+2

rowconfigureを追加して垂直展開も可能にする –

0

私はこのためにパックを使用します。

は、ここに私のコードです。ほとんどの場合、それで十分です。 両方を混ぜないでください!

class Application(Frame): 
    def __init__(self, master=None): 
     Frame.__init__(self, master) 
     self.pack(fill = X, expand =True) 
     self.createWidgets() 

    def createWidgets(self): 
     self.dataFileName = StringVar() 
     self.fileEntry = Entry(self, textvariable=self.dataFileName) 
     self.fileEntry.pack(fill = X, expand = True) 
     self.loadFileButton = Button(self, text="Load Data",) 
     self.loadFileButton.pack(fill=X, expand = True) 
+0

グリッドの代わりにpackを使用しますが、私がグリッドを読み込んだものは、ウィジェットのサイズ変更にも役立つはずです。 – mjn12

+0

私は長い間グリッドを使用していなかったので、私は知らない。がんばろう! – User

0

実例。使用する列と行ごとにconfigureを明示的に設定する必要があることに注意してください。ただし、下のボタンのcolumnspanは表示される列の数よりも大きい数です。

## row and column expand 
top=tk.Tk() 
top.rowconfigure(0, weight=1) 
for col in range(5): 
    top.columnconfigure(col, weight=1) 
    tk.Label(top, text=str(col)).grid(row=0, column=col, sticky="nsew") 

## only expands the columns from columnconfigure from above 
top.rowconfigure(1, weight=1) 
tk.Button(top, text="button").grid(row=1, column=0, columnspan=10, sticky="nsew") 
top.mainloop() 
関連する問題