Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I position my tkinter widgets right?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading