Pygame window keeps scales weirdly - python

For some reason when ever I make a program with pygame, the window is bigger then I make it but all the coordinates are okay and it's been annoying me a lot. Can anyone help with this?
Here is a part of code I use to test it:
WIN = pygame.display.set_mode() # Makes a window the size of the screen
print(f'Window size: {WIN.get_size()}\nScreen size: {pygame.display.list_modes()[0]}') # Displays window size and screen size
Here is the output:
Window size: (1536, 864)
Screen size: (1920, 1080)
Edit: putting (1536, 864) doesn't work

replace WIN = pygame.display.set_mode() with WIN = pygame.display.set_mode((1536, 864)) . So what I think is the problem is that the screen's size is not specified. Hence the weird scaling.

Related

How to make a window fullscreen in customtkinter?

Tkinter has this method:
window_name.attributes('-fullscreen',True)
and customtkinter?
I haven't found anything but
geometry(f"{WIDTH}x{HEIGHT}")
However when I grab screen size and put as WIDTH & HEIGHT the screen does change the size but does not go full size, I mean it is always shifted to the right so the windows leaves the margin on the left and top and the part of the window goes out of the screen to the right.

How to make pygame window appear in a certain part of screen? [duplicate]

I need the window position right after I created a pygame window:
window = pygame.display.set_mode((width, height), 0, 32)
pygame.init()
By default, the window starts at 0,0 - but I also need x,y if the user changes the window position. Any ideas?
I need x,y coords of the pygame window - either at start or on window move. The last one is nice to have.
I figured out how to center the pygame window at the bottom of the screen:
pos_x = screen_width / 2 - window_width / 2
pos_y = screen_height - window_height
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y)
os.environ['SDL_VIDEO_CENTERED'] = '0'
Background: I have x,y coords which are screen related and I must convert the screen coords into window-local coords so that I can use them e.g. to display coords inside the pygame window or to discard coords which are outside the pygame window.
With my approach above, I knwo the initial position. But I can only use a single pygame window (because it's always at the same position) and things go wrong if the user moves the window.
It have worked for me :
import os
import pygame
x = 100
y = 45
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
pygame.init()
screen = pygame.display.set_mode((100,100))
Taken from https://www.pygame.org/wiki/SettingWindowPosition
Here is an example code that return all four corner positions:
from ctypes import POINTER, WINFUNCTYPE, windll
from ctypes.wintypes import BOOL, HWND, RECT
# get our window ID:
hwnd = pygame.display.get_wm_info()["window"]
# Jump through all the ctypes hoops:
prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
paramflags = (1, "hwnd"), (2, "lprect")
GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
# finally get our data!
rect = GetWindowRect(hwnd)
print "top, left, bottom, right: ", rect.top, rect.left, rect.bottom, rect.right
# bottom, top, left, right: 644 98 124 644
There is a pygame.display.get_wm_info() call that gets you the Window handler -- from then on, it is using either X11 or Windows API32 to get information from the window through this handler. I didn't find any readily available information on how to do that.
So, just to be clear: there is no ordinary way to do that from within pygame. You have to proceed with another library, possibly using ctypes, after you get the window handler.
On the other hand, if you have to manipulate the window itself, maybe pygame is not the most suitable library for you to use -- you could try PyQt or even GTK+ - they also provide multmedia facilites while being more proper to operate on the level of GUI Windows and other controls
update There are ways to setup an OpenGL backend for pygame graphics, that will allow complete control of the display - including embedding it in another window, as part of a tkinter or Qt application. People that are interested can search a little deeper along those lines.
In Pygame 2, you can alternatively import the _sdl2.video to set the window position. Note that this module is experimental and could be changed in future versions.
import pygame
from pygame._sdl2.video import Window
pygame.init()
window = Window.from_display_module()
window.position = your_position_tuple
Using environment variables as mentioned in other answers is sufficient for most cases, but the downside is that once you have change the window position it will not work a second time (at least in Pygame 2).
Pygame is based on Simple DirectMedia Layer (SDL). Hence you can set SDL environment variables.
See pygame wiki - SettingWindowPosition:
You can set the position of the window by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.
x = 100
y = 0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
import pygame
pygame.init()
screen = pygame.display.set_mode((100,100))
(0,0) remains the upper left corner whether the window is moved or not. If you're trying to make (0,0) stay physically where it was on the screen when the window initialized, I don't think pygame can do that. Try to make your question clearer if you want clearer answers.
To accomplish this where you don't know the monitor size of the user, use screeninfo in addition to the pygame and os packages. Screeninfo is OS-agnostic, meaning you can get the resolution of all monitors regardless of a users operating system.
import pygame
from screeninfo import get_monitors
import os
# Set the size of the pygame window
window_width = 512
window_height = 288
window_size = (window_width, window_height)
# Get the bounds of the users monitors, and select the first one
monitors = get_monitors() # Get the resolution of all of the users monitors
screen_width = monitors[0].width # Get width of first monitor found
screen_height = monitors[0].height # Get height of first monitor found
# Set the x and y coordinates of the pygame window in relation to the monitor's resolution
# (I wanted my pygame window to be located in the bottom-right of the monitor)
pos_x = screen_width - window_width # Calculate the x-location
pos_y = screen_height - window_height # Calculate the y-location
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y) # Set pygame window location
pygame.init() # Initialize the pygame window
self.screen = pygame.display.set_mode(size) # Set the location of the pygame window

pygame window get position [duplicate]

I need the window position right after I created a pygame window:
window = pygame.display.set_mode((width, height), 0, 32)
pygame.init()
By default, the window starts at 0,0 - but I also need x,y if the user changes the window position. Any ideas?
I need x,y coords of the pygame window - either at start or on window move. The last one is nice to have.
I figured out how to center the pygame window at the bottom of the screen:
pos_x = screen_width / 2 - window_width / 2
pos_y = screen_height - window_height
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y)
os.environ['SDL_VIDEO_CENTERED'] = '0'
Background: I have x,y coords which are screen related and I must convert the screen coords into window-local coords so that I can use them e.g. to display coords inside the pygame window or to discard coords which are outside the pygame window.
With my approach above, I knwo the initial position. But I can only use a single pygame window (because it's always at the same position) and things go wrong if the user moves the window.
It have worked for me :
import os
import pygame
x = 100
y = 45
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
pygame.init()
screen = pygame.display.set_mode((100,100))
Taken from https://www.pygame.org/wiki/SettingWindowPosition
Here is an example code that return all four corner positions:
from ctypes import POINTER, WINFUNCTYPE, windll
from ctypes.wintypes import BOOL, HWND, RECT
# get our window ID:
hwnd = pygame.display.get_wm_info()["window"]
# Jump through all the ctypes hoops:
prototype = WINFUNCTYPE(BOOL, HWND, POINTER(RECT))
paramflags = (1, "hwnd"), (2, "lprect")
GetWindowRect = prototype(("GetWindowRect", windll.user32), paramflags)
# finally get our data!
rect = GetWindowRect(hwnd)
print "top, left, bottom, right: ", rect.top, rect.left, rect.bottom, rect.right
# bottom, top, left, right: 644 98 124 644
There is a pygame.display.get_wm_info() call that gets you the Window handler -- from then on, it is using either X11 or Windows API32 to get information from the window through this handler. I didn't find any readily available information on how to do that.
So, just to be clear: there is no ordinary way to do that from within pygame. You have to proceed with another library, possibly using ctypes, after you get the window handler.
On the other hand, if you have to manipulate the window itself, maybe pygame is not the most suitable library for you to use -- you could try PyQt or even GTK+ - they also provide multmedia facilites while being more proper to operate on the level of GUI Windows and other controls
update There are ways to setup an OpenGL backend for pygame graphics, that will allow complete control of the display - including embedding it in another window, as part of a tkinter or Qt application. People that are interested can search a little deeper along those lines.
In Pygame 2, you can alternatively import the _sdl2.video to set the window position. Note that this module is experimental and could be changed in future versions.
import pygame
from pygame._sdl2.video import Window
pygame.init()
window = Window.from_display_module()
window.position = your_position_tuple
Using environment variables as mentioned in other answers is sufficient for most cases, but the downside is that once you have change the window position it will not work a second time (at least in Pygame 2).
Pygame is based on Simple DirectMedia Layer (SDL). Hence you can set SDL environment variables.
See pygame wiki - SettingWindowPosition:
You can set the position of the window by using SDL environment variables before you initialise pygame. Environment variables can be set with the os.environ dict in python.
x = 100
y = 0
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (x,y)
import pygame
pygame.init()
screen = pygame.display.set_mode((100,100))
(0,0) remains the upper left corner whether the window is moved or not. If you're trying to make (0,0) stay physically where it was on the screen when the window initialized, I don't think pygame can do that. Try to make your question clearer if you want clearer answers.
To accomplish this where you don't know the monitor size of the user, use screeninfo in addition to the pygame and os packages. Screeninfo is OS-agnostic, meaning you can get the resolution of all monitors regardless of a users operating system.
import pygame
from screeninfo import get_monitors
import os
# Set the size of the pygame window
window_width = 512
window_height = 288
window_size = (window_width, window_height)
# Get the bounds of the users monitors, and select the first one
monitors = get_monitors() # Get the resolution of all of the users monitors
screen_width = monitors[0].width # Get width of first monitor found
screen_height = monitors[0].height # Get height of first monitor found
# Set the x and y coordinates of the pygame window in relation to the monitor's resolution
# (I wanted my pygame window to be located in the bottom-right of the monitor)
pos_x = screen_width - window_width # Calculate the x-location
pos_y = screen_height - window_height # Calculate the y-location
os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (pos_x,pos_y) # Set pygame window location
pygame.init() # Initialize the pygame window
self.screen = pygame.display.set_mode(size) # Set the location of the pygame window

Full Screen Gui With Different Screen Sizes In PC Python

Situation:
I'm making a software that has to be full screen
It is all UIs and interfaces.
I want it to work on computers with screens that have different resolutions - So I need the GUI to adjust to the screen size: text will be smaller if the screen resolution is smaller, but it will still be in the middle of the screen
I've tried not using numbers in deciding the position of the text, but instead getting the screen resolution, and multiplying it
Problem:
The text is not getting smaller.
Question:
Is there an easy solution for my problem? Is there a module in python for this purpose? I'm currently using WxPython but I'm open to use any other GUI module.
def Title(object):
(sizeX, sizeY) = object.GetSize()
(displayX, displayY) = wx.GetDisplaySize()
print(displayX)
print(displayY)
print(sizeX)
print(sizeY)
object.SetPosition((displayX/2 - sizeX/2, displayY*0.01))
To adjust the text size to be appropriate to the screen size, you would have to define a custom font size.
Although I suspect that the default font size will already be roughly correct, as the user will have set the system font size, based on the screen size.
You can get the current font size as follows:
self.font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
point_size = self.font.GetPointSize()
Define a new, appropriate, font size based on the result from wx.GetDisplaySize():
self.font.SetPointSize(new size)
Then use SetFont(font) on the items in your UI:
self.panel.SetFont(self.font)

Can you obtain physical size of device in kivy?

Does anyone know how Kivy renders text in different fonts?
I have labels with text at 16sp.
On a tablets with screen sizes (1024, 552) and (2048, 1536) it works perfectly (width/height ratios 1.855 and 1.333 respectively)
On the pc with screen size (1280, 720) (ratio 1.778) it also displays perfectly, but on a phone with this screen size the letters are truncated
The only difference here is the physical size of the device. It thus appears, that Kivy text is not rendered according to some algorithm based on pixels, but takes into account the physical size of the screen
Is there any way to determine the physical size in Kivy? and hence allow me to adjust font size accordingly. The text appears correctly on the phone (small device) if I use a smaller (10sp) font size, but then it appears too small on the larger devices.
Yes, you can determine the physical size of screen with kivy -
You could simply use this Module:
(from kivy.core.window import Window)
and then determine sizes by writing this:
(Window.size)
Check this code out (this code determines screen physical sizes on a simple label):
⬇️⬇️
from kivy.app import App
from kivy.uix.label import Label
from kivy.core.window import Window
class MyApp(App):
def build(self):
window_sizes=Window.size
return Label(text="screen sizes= "+str(window_sizes))
MyApp().run()
It is possible to obtain the screen sizes, but you'll need to install plyer and before packaging also patch it, so that you could access the info.
Or just use the plain pyjnius and access android.util.DisplayMetrics with autoclass.
From what I am understanding, you are having issues displaying your text properly, over multiple devices. Now, I do not think that you can get the devices actual dimensions through Kivy itself, but with plain old python, you can.
An Example in Python:
import gtk
window = gtk.Window()
screen = window.get_screen()
print "screen size: %d x %d" % (
gtk.gdk.screen_width(),gtk.gdk.screen_height())
The Code above will print out the screen heigh and width, or resolution, for you to use.
To store them in variables instead:
import gtk
window = gtk.Window()
screen = window.get_screen()
ScreenWidth = gtk.gdk.screen_width()
ScreenHeight = gtk.gdk.screen_height()
# And to prove it worked (You would not want to include this part in code
#########################################################################
print str(ScreenWidth) + 'x' + str(ScreenHeight)
Now that you have those variables, you can use them in your .kv file, by pulling them from a python file, or implementing the whole function directly into your Kivy Code somehow.
From: https://kivy.org/docs/api-kivy.metrics.html we learn that 'sp' is the scale independent pixel size. Thus it includes the pixel density already and the font size appears the same on each device (at least should).
The reason, why your font is clipped is because of the size of the container. You also should also give the desired size, like in this example (note: if you want a secific height, you must include the size_hint_y: None argument)
TextInput:
id: text_input
size_hint_y: None
height: '30sp'
multiline: False
What I ended up doing is maximizing my window to get the maximum allowed non-full screen size, then reading that size. I actually used it to center a smaller window on screen:
#-- maximize first, to get the screen size, minus any OS toolbars
Window.maximize()
maxSize = Window.system_size
#-- set the actual window size, to be slightly smaller than full screen
desiredSize = (maxSize[0]*0.9, maxSize[1]*0.9)
Window.size = desiredSize
#-- center the window
Window.left = (maxSize[0] - desiredSize[0])*0.5
Window.top = (maxSize[1] - desiredSize[1])*0.5
Note that by maximizing my window first, I am getting the maximum allowed size, less any operating system's toolbars etc., i.e., the size that the window has when you press the maximize button.

Categories

Resources