I am making a simple tkinter app and I want to make the layout look better.
The layout currently looks like this:
Simple Image
But I want it to look like this:
Wanted Image
The code for it is right here:
self.max_length = Entry(self);
self.max_length.pack(side = LEFT);
self.load_button = Button(self, text = "Load Story", command = self.generate_story);
self.load_button.pack(side = LEFT);
self.script_template = Text(self);
self.script_template.pack(side = TOP);
Any way to make the layout like what I want?
>Solution :
import tkinter as tk
root = tk.Tk()
top_frame = tk.Frame(root, bd=0, highlightthickness=0)
top_frame.pack(side="top")
max_length = tk.Entry(top_frame)
max_length.pack(side="left")
load_button = tk.Button(top_frame, text="Load Story")
load_button.pack(side="right")
script_template = tk.Text(root)
script_template.pack(side="bottom")
root.mainloop()
An answer that uses a frame to hold the entry and button. To make the widgets expand, you can use pack‘s fill/expand parameters. For more info on expand vs fill, look at this.
With this answer the main window has only 2 widgets (the frame and the text widget). Inside the frame widget there are 2 more widgets (the entry and button).
This can also be done by replacing pack with grid but will leave someone else to write that answer.