The following code will place the window in a random position. I want it to use the fixed position I specified.
My screen is a TV with a 3840x2160 HDMI connection and a second screen 1920x1080. Windows 11 does return the combined size 5760x2160.
class TkConfigParameters(tk.Tk):
def __init__(self, shared_world_inst):
super().__init__()
self.shared_world_inst = shared_world_inst
self.shared_utils_inst = SharedUtils(self.shared_world_inst) # Create an instance of SharedUtils
print('in TkConfig_Parameters\n')
print(f'Type of shared_world_inst: {type(self.shared_world_inst)}')
self.shared_utils_inst.print_niao() # Call the print_niao method
# get screen width and height
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
self.title("Configuration Parameters")
# Set the window size
width = 600
height = 300
self.geometry(f"{width}x{height}") # Set geometry without position
# Update the window to process the geometry
self.update_idletasks() # Ensure the geometry is processed
# Set fixed position
x = 100 # Fixed x-coordinate for the window position
y = 100 # Fixed y-coordinate for the window position
self.geometry(f"+{x}+{y}") # Set only the position
Your code is setting the window position in two separate .geometry()
calls - one for size, then one for position, and that second call (self.geometry(f"+{x}+{y}")
) actually wipes out the size you set earlier.
This can cause bugs and result in the window not appearing in the correct place. So, to make your window always open in the same place with the same size, you can combine them:
width = 600
height = 300
x = 100 # X-coordinate
y = 100 # Y-coordinate
self.geometry(f"{width}x{height}+{x}+{y}")
In Tkinter, .geometry()
takes both size and position in one string:
"{width}x{height}+{x}+{y}"