I want to set default tkinter screen and not full screen in tkinter.Thank you
root.geometry("500x100") #Width x Height
Use this code to change your Tkinter screen size. Here root is the main window, geometry() is the function used to change screen size. 500 is the width and 100 is the height
its very easy you just need to set the with and heigth of the window by
root.geometry("400x900") # this .geometry() takes a string in the form of width x heigth
You have various arguments for your Tkinter window :
window.attributes("-fullscreen", True)
Changing it to False will disable fullscreen mode, True will activate it
window.geometry("1400x700")
You can set the default size of your GUI with this, but the user will be able to resize it classically
window.minsize(200, 200)
This one will set the minimum size of your GUI, past it, the user will not be able to reduce the window any further. If you set it to the size of your window, you disable the resizing.
Related
I have a tkinter window that has been scaled using ctypes. When I try to make the window full-screen using the "-fullscreen" attribute, the window does not resize to cover the screen and instead, only covers half the screen.
Example:
import tkinter as tk
import ctypes
window = tk.Tk()
window.tk.call('tk', 'scaling', 2) ## Double the scale of the window
ctypes.windll.shcore.SetProcessDpiAwareness(2) ## Double the resolution of the window
## Window now has double resolution with 1x scale
window.attributes("-fullscreen", True) ## Making window full-screen doesn't work properly
window.mainloop()
Also, I don't know if this is related, but winfo_screenwidth/height returns an incorrect value for the screen width and height.
Thanks for any help.
How about replacing
window.attributes("-fullscreen", True)
with
window.geometry(f"{window.winfo_vrootwidth()}x{window.winfo_screenheight()}+{-7}+0")
did you?
I have created an application that required to use the place() manager. I tried using pack() and grid() but after many tries and lots of effort it did not work for my goals. Since I use relx= and rely= almost all the time and I want to put my app on multiple OS's I need to have the window resize to fit all the widgets without them touching.
Is there a way to do this? Since many OS's and their updates change rendering the sizes of widgets changes greatly and I don't want the user to have to resize the window all the time. I want it to just fit as tightly as possible or get the minimum width and height to I can add some buffers. Is this possible? If not, is there a way to fix my app without having to rewrite everything?
Note:
I found something similar: Get tkinter widget size in pixels, but I couldn't properly get it to work.
I figured it out. You can use the widget.winfo_height()/widget.winfo_width() functions to get the minimum pixel sizes. Since the window.geometry() function requires pixel sizes you can make two lists: one for width, and one for height. By adding the minimum amount of widgets you can get the desired size.
Code:
height_widget_list = [main_label, main_notebook, the_terminal, quit_button, settings_button]
width_height_list = [commands_label, main_notebook, quit_button]
widget_list = [main_label, main_notebook, the_terminal, quit_button, settings_button, commands_label]
# Widgets must be updated for height and width to be taken
for widget in widget_list:
widget.update()
height_required_list = []
width_required_list = []
# Collects data
for widget in height_widget_list:
height_required_list.append(int(widget.winfo_height()))
for widget in width_height_list:
width_required_list.append(int(widget.winfo_width()))
# Get height requirement
minimum_height = 0
for height in height_required_list:
minimum_height += height
# Get width requirement
minimum_width = 0
for width in width_required_list:
minimum_width += width
# Make window correct size make window require the correct sizing
window.geometry("{}x{}".format(minimum_width, minimum_height))
window.resizable(False, False)
How can I do (if any user try to resize window width will automatically resize window height too with aspect ratio. That's mean user can't be able to resize only one size. changing one side size will change another side too.)?
(After closely reading the comment you made under your question describing how you wanted this to work, I've revised the code once again to make it so.)
You can do it by handling '<Configure>' events in an event handler bound to the root window. This is tricky because when you add a binding to the root window, it also gets added to every widget it contains — so there's a need to be able to differentiate between them and the root window in the event handler. In the code below this is done by checking to see if the event.widget.master attribute is None, which indicates the widget being configured is the root window, which doesn't have one.
The code creates the root window and binds root window resizing events to an event handler function that will maintain the desired aspect ratio. When a resize event of the root window is detected, the function checks the aspect ratio of the new size to see if it's has the proper ratio, and when it's doesn't it blocks further processing of the event, and manually sets the window's size to a width and height that does.
import tkinter as tk
WIDTH, HEIGHT = 400, 300 # Defines aspect ratio of window.
def maintain_aspect_ratio(event, aspect_ratio):
""" Event handler to override root window resize events to maintain the
specified width to height aspect ratio.
"""
if event.widget.master: # Not root window?
return # Ignore.
# <Configure> events contain the widget's new width and height in pixels.
new_aspect_ratio = event.width / event.height
# Decide which dimension controls.
if new_aspect_ratio > aspect_ratio:
# Use width as the controlling dimension.
desired_width = event.width
desired_height = int(event.width / aspect_ratio)
else:
# Use height as the controlling dimension.
desired_height = event.height
desired_width = int(event.height * aspect_ratio)
# Override if necessary.
if event.width != desired_width or event.height != desired_height:
# Manually give it the proper dimensions.
event.widget.geometry(f'{desired_width}x{desired_height}')
return "break" # Block further processing of this event.
root = tk.Tk()
root.geometry(f'{WIDTH}x{HEIGHT}')
root.bind('<Configure>', lambda event: maintain_aspect_ratio(event, WIDTH/HEIGHT))
root.mainloop()
The following will set a tkinter window width and height.
root.geometry("500x500")
Is it possible to only set width or height?
Where can I find a full list of geometry method variations, for setting only select parameters?
I would like to be able to set select parameters, of the window size and/or position, and let the rest be under tkinter dynamic control.
When looking at the tcl/tk documentation, we can see that we do not have to provide a full "widthxheight±x±y" string to the .geometry() method:
with only "widthxheight", the size of the window will be changed but not its position on the screen.
with only "±x±y", the position will be changed but not the size.
However, it is not possible to set separately the width and height of the window with this method. Nevertheless, you can retrieve the dimension you don't want to change and use it in .geometry() with something like
def set_height(window, height):
window.geometry("{}x{}".format(window.winfo_width(), height))
def set_width(window, height):
window.geometry("{}x{}".format(width, window.winfo_height()))
Note: Using root.config(width/height=...) only works for me if the window has never been resized with the mouse or using .geometry()
Use root.config(width=100) or root.config(height=100)
If you define a frame within your window you can set width and height of this frame separately like this:
myframe = tk.Frame(width=200)
This may create the appearance you wish to achieve.
An example program demonstrates how to use this trick:
import tkinter as tk
window = tk.Tk()
window.title('Frame Demo')
frame = tk.Frame(width=300)
frame_2 = tk.Frame()
label = tk.Label(master=frame_2, text="Frame_2")
label.pack()
frame.pack()
frame_2.pack()
window.mainloop()
The frame by the name frame is used only to widen the window and is invisible. No GUI elements should be placed in it. Its only function is to give the main window the needed size of 300, in this example. frame_2 is the canvas, so to say, for the GUI elements.
Apologies for not being very clear the first time round.
I am looking for a way to specify the size of "fullscreen" for a window. Is there a way for me to do this?
I have a window that opens off fullscreen mode. When I put the window in full screen mode, the bottom of the window goes behind the taskbar. Is there a way that I can avoid this.
Note that I don't know the size of the screen or taskbar beforehand.
you can simply write:
from tkinter import *
root = Tk()
height = root.winfo_screenheight()
width = 400 #or whatever you want the width to be
root.resizable(height = height, width = width)