How to manage the size of tkinter windows across many display of different sizes
For example in a device with resolution 1600x900 I have two adjacent windows with sizes 900x900 and 700x900(side to side)
from tkinter import *
root1=Tk()
root1.geometry("900x900")
root1.geometry("+0+0")
# some labels inside root1 with some width
root2=Tk()
root2.geometry("700x900")
root2.geometry("+900+0")
# some labels inside root2 window with width
root1.mainloop()
root2.mainloop()
If this code is used in a display with resolution 1300x900 some part of the second window gets hidden. I attempted to get resolution of device using root.winfo_screenwidth() and root.winfor_screenheight() but that doesn't solve the problem for managing the size of labels. So is there a way to shrink the whole window to an affordable size along with the labels, buttons and other things without affecting any position of labels.
Related
Hi I made the GUI of this program in 1980x1080 and everything is fit to the window, but when I run the program on the other pc of resolution 1366x768 3/4 of the GUI is outside the display.
This is my constructor for the GUI
class Main:
def __init__(self,master = None):
self.master = master
self.master.geometry(str(master.winfo_screenwidth())+'x' +str(master.winfo_screenheight()))
self.master.state('zoomed')
self.variables_for_graphs()
self.variables_for_inputer()
self.variables_for_graphs_menu()
self.place_frames()
screenshot of the program in 1980x1080 display resolution:
screenshot of the program in 1366x768 display resolution:
I add scaling to the constructor
dpi = master.winfo_fpixels('1i')
factor = dpi / 72
master.tk.call('tk', 'scaling', factor)
it did not help
if anything else is need please comment
you can multiply every dimension in your GUI by the ratio between the current resolution and the original resolution, but that would result in very ugly look, instead of manually setting the sizes of each object you should let tkinter size your objects.
for grid layout (similar to yours) you have. Tk Geometry manager
widget1.grid(row=0,column=0,rowspan=2,sticky="NWSE")
widget2.grid(row=0,column=1,sticky="NWSE")
widget3.grid(row=1,column=1,sticky="NWSE")
parent_widget.rowconfigure((0,1),weight=1,minsize=200)
parent_widget.columnconfigure((0,1),weight=1,minsize=200)
to set your widgets to grow to fill all its master space, with a minimum size of 400x400.
for widgets that you pack you should use
widget.pack(expand=True,fill="both")
which will also make the child grow to fill its parent space, the size system may look intimidating at first, but once you start using it you can get any shape you want with it that scales the way you want.
avoid using place as you have to manually manage its size (unless you have an object that must be of fixed size and position like a logo or floating widget), and avoid filling numbers for sizes yourself, things that you can fill yourself are probably padding, buttons size (sometimes), and font size (under different dpi), you should have tkinter manage other dimensions in your GUI otherwise your GUI won't scale well.
I am making a GUI with several buttons, text boxes,... in Python using TKinter. At this moment, all sizes of the components are fixed, so whenever the main frame is resized, the components do not adjust to the size of the main frame. As a result, when the frame reaches a certain size, components disappear.
Is there an easy solution that makes the sizes of the components dynamic and proportional to the size of the main frame?
The pictures below speak for themselfs...
Thanks in advance!
I'm trying to creat a python interface with tkinter.
The code is done on my laptop so every element's size (frame, label, entry, button, text font size, etc.) was created to fit my laptop's screen. I places all elements by grid.
But when i run the script on another computer with smaller screen size, i could not see the whole interface.
Is there any way in tkinter to automatically resize (reduce text size, frame size, entry size) to fit the interface to the screen size.
Thanks for your help!
My code is the following:
import tkinter as tk
#setting up window.
root = tk.Tk()
root.title("CSV Maker")
root.geometry("600x300")
#setting up frames.
leftFrame = tk.Frame(root, bg="red", width=300, height=300)
rightFrame = tk.Frame(root, bg="blue", width=300, height=300)
#placing frames on window.
leftFrame.grid(row=0, column=0)
rightFrame.grid(row=0, column=1)
#setting up labels.
inputPathLabel = tk.Label(leftFrame, text="Input File Path:")
#placing labels on frames.
inputPathLabel.grid(row=0, column=0)
root.mainloop()
When I remove the label I get the following:
Without label
However when I leave the code as it is below (with a label), I get a completely different result. It seems as if the frame was resized to another size than the one that I selected and the color is gone. Why is this?
With label
That is simply how tkinter was designed to work. When you use pack or grid, frames (or any other widget) will shrink or expand to try to fit all of its contents.
99.9% of the time, this is the behavior you want. Tkinter is really good at making GUIs the appropriate size.
From the official documentation for grid:
The grid geometry manager normally computes how large a master must be to just exactly meet the needs of its slaves, and it sets the requested width and height of the master to these dimensions. This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows. However, the grid propagate command may be used to turn off propagation for one or more masters. If propagation is disabled then grid will not set the requested width and height of the master window. This may be useful if, for example, you wish for a master window to have a fixed size that you specify.
From the documentation for pack:
The packer normally computes how large a master must be to just exactly meet the needs of its slaves, and it sets the requested width and height of the master to these dimensions. This causes geometry information to propagate up through a window hierarchy to a top-level window so that the entire sub-tree sizes itself to fit the needs of the leaf windows. However, the pack propagate command may be used to turn off propagation for one or more masters. If propagation is disabled then the packer will not set the requested width and height of the packer. This may be useful if, for example, you wish for a master window to have a fixed size that you specify.
Notice that place doesn't have the same behavior. From the place documentation:
Unlike many other geometry managers (such as the packer) the placer does not make any attempt to manipulate the geometry of the master windows or the parents of slave windows (i.e. it does not set their requested sizes). To control the sizes of these windows, make them windows like frames and canvases that provide configuration options for this purpose.
I have a simple pygtk/glade window with a menu and a 3x3 grid. Each row of the grid consists on: two labels and a button.
When the Window is resized, the labels holds the same font size, but the buttons get resized, and they could become HUGE if the windows gets very big.
How could I manage to keep my buttons with the same size always (the "standar" size of a button, just like they are when the interface is just opened) no matter if the Window is resized?
You just have to set the fill and expand parameters of the Buttons to False (uncheck them in the Glade interface).
You would also want to put each button at the center of a 3x3 GtkTable, so it will appear centered and not aligned at the top of the cell