0

My desktop resolution is 1920x1080 and I am developing for an environment which is the same, 1440x900 or less (it depends on user's screen resolution) When I am on 1920x1080 resolution, all widgets are visible but when the resolution change, some widget are not visible. However I used grid method. What to do?

Here is an example code:

from tkinter import *
import tkinter as tk


root = Tk()
width=1440
height=900
root.geometry('%dx%d+0+0' % (width,height))
print(width, height)

frame1=LabelFrame(root, text="frame1")

frame2=LabelFrame(root, text="frame2")

frame1.pack(fill="both", expand="yes", padx=20, pady=10)

frame2.pack(fill="both", expand="no", padx=20, pady=10)



abc=StringVar()
Label(frame2, text="A").grid(row=1, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=1, padx=5, pady=3,sticky="nsew")

Label(frame2, text="B").grid(row=1, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=3, padx=5, pady=3,sticky="nsew")

Label(frame2, text="C").grid(row=1, column=4, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=5, padx=5, pady=3,sticky="nsew")

Label(frame2, text="D").grid(row=1, column=6, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=1, column=7, padx=5, pady=3,sticky="nsew")



Label(frame2, text="E").grid(row=2, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=1, padx=5, pady=3,sticky="nsew")

Label(frame2, text="F").grid(row=2, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=3, padx=5, pady=3,sticky="nsew")

Label(frame2, text="G").grid(row=2, column=4, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=5, padx=5, pady=3,sticky="nsew")

Label(frame2, text="H").grid(row=2, column=6, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=2, column=7, padx=5, pady=3,sticky="nsew")



Label(frame2, text="I").grid(row=4, column=0, padx=5, pady=3)
bla=Text(frame2, width=220, height=2.5, font=("Calibri", 14))
bla.grid(row=4, column=1, columnspan=11, padx=5, pady=3, sticky="nsew")

Label(frame2, text="J").grid(row=3, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=3, column=1, padx=5, pady=3,sticky="nsew")



Label(frame2, text="K").grid(row=3, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=3, column=3, padx=5, pady=3,sticky="nsew")


Label(frame2, text="L").grid(row=5, column=0, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=1, padx=5, pady=3,sticky="nsew")

Label(frame2, text="M").grid(row=5, column=2, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=3, columnspan=2, padx=5, pady=3,sticky="nsew")

Label(frame2, text="N").grid(row=5, column=5, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=6, padx=5, pady=3,sticky="nsew")

Label(frame2, text="O").grid(row=5, column=7, padx=5, pady=3)
Entry(frame2, textvariable=abc).grid(row=5, column=8,columnspan=3, padx=5, pady=3,sticky="nsew")


root.mainloop()

Thank u

8
  • Why don't you change the width of the entries or change the font for the labels+entried? You can also remove the padding that you put there. Commented May 4, 2021 at 11:20
  • Hello TheLizzard. Thank you. But how do I know the right width to fit all screen? Commented May 4, 2021 at 11:31
  • You will have to create your own code that does that or just guess. Commented May 4, 2021 at 11:33
  • so there is no function to adjust automatically the size of buttons, image automatically? Commented May 4, 2021 at 11:39
  • Nope. tkinter doesn't automatically resize tk.Entry and tk.Label. You can just get the width/height of the screen using tkinter and then add the widgets based on that. Commented May 4, 2021 at 11:41

1 Answer 1

2

The problem is that you're explicitly setting the text widget width to 220 characters. Depending on the font, that may make the widget wider than 1440 pixels. On my machine, the widget ends up being 1988 pixels. Because of that, it forces the columns to be wider than will fit in the window.

The simple solution in this specific case seems to be to not explicitly set the size of the text widget. Leave it at its natural size and let the geometry manager cause it to expand to fill the window. If you truly need it to be 220 characters wide, you will need to make the font smaller so that that it will fit within the constraints of the window.

As a general rule of thumb, you should set the size of a widget to its minimum required size, and let the geometry managers be in control of making them large enough to fill any extra space.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. But "let the geometry manager cause it to expand to fill the window" how to do that? I want the text widget to expand to the screen wide. But when I do that it mask the entry or button below...
@Béa: you let the geometry manager to it by using the options such as fill and expand with pack, sticky and grid_rowconfigure and grid_columconfigure with grid, and with relative placement, widths and heights with place.
An example pls....I tried to use grid method but the text widget don't expand to the screen wide (in second frame)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.