6

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

これが私のコードです:

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

3 に答える 3

15

ルートウィンドウを追加し、Frameウィジェットも拡張するようにcolumnconfigureします。これが問題です。ルートウィンドウを指定せず、フレーム自体が適切に拡張されていない場合は、暗黙のルートウィンドウが表示されます。

root = Tk()
root.columnconfigure(0, weight=1)
app = Application(root)
于 2012-05-05T19:17:47.967 に答える
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)
于 2012-05-05T19:11:02.693 に答える
0

実例。使用する列と行ごとに構成を明示的に設定する必要がありますが、下のボタンの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()
于 2014-11-28T22:23:35.940 に答える