Get screen resolution in linux (python)

hi i’m using this linux command to get the resolution:

xrandr  | grep \* | cut -d' ' -f4

but if i do this:

resolution = os.system("xrandr  | grep \* | cut -d' ' -f4")
print resolution

it will print this:


and the resolution = 0
what can i do to asign the real resolution to variable resolution?

i just tried your code on ubuntu and i also get resolution=0 . i think os.system returns some sort of process id and thats why it is 0.:no:

but i also tried

import os
os.system("xrandr  | grep \* | cut -d' ' -f4 >resolution.txt")

and now a file named resolution.txt is created with the resolution 1366x768 written on it.

may be there is an easier way to do this. i will check that.:wink:

hey thanks now it’s working… i dont have to do different way… you will only have one more file in the /home/user/.biw-game/ :smiley:

btw: i’m using shell script that the game modified on every exit… and do you think that using xrandr is safe? but it will use it only if you toggle the fullscreen on… there are two examples of the script…

fullscreen off

/usr/share/biw-game/BiW64 -w 800 600 -g show_framerate = 1

fullscreen on

xrandr -s 1280x960& 
/usr/share/biw-game/BiW64 -f 1280 960 -g show_framerate = 1
xrandr -s 1280x1024

now i have only one thing to solve… how to hide gnome panel in fullscreen :smiley:

for hiding the gnome panel i use a keyboard shortcut assigned by myself.

it is “Toggle fullscreen mode” under Window Management on System -> Preferences ->Keyboard Shortcuts

i use it to hide the panel just after the game run.

also check this out.

yes i do the same :smiley: but how can i do this if the user haven’t the shortcut defined… just a little workaround to work on every ubuntu…

I think the ubuntu community at might come up with a better help with keyboard shortcut tweak

heh when i trying to change resolution with pygame module… it only open the fullscreen black window and i have to reset the pc :smiley: i cant do anything bcs the game was behind that window and it grab my mouse and keyboard… so i decided to stay with the blendergame “fullscreen” window :smiley:

wuaaa. i have solved the fullscreen window :wink: it’s not so clear but it’s working… i’ll have distributing the game asi .deb package so i can add more dependencies or files… so i add the wmctrl binary to the package and bcs i have the 32 / 64 bit binaries for linux in one package name it wmctrl32 and wmctrl64… the wmctrl is the tool that can set the window hint, such a fullscreen and etc… so I using this script to make it working…

import platform

arch = platform.architecture()[0].replace ("bit", "")

if settings["windowed"] == "-f":
		os.system("/usr/share/biw-game/wmctrl" + arch + " -r BiW" + arch + " -b toggle,fullscreen")
		print "executing: " + "/usr/share/biw-game/wmctrl" + arch + " -r BiW" + arch + " -b toggle,fullscreen")

the -r after the wmctrl is the window name., and the -b toggle,fullscreen is understandable :smiley:

but you have to execute game in window… bcs in fullscreen the window have no name… so for linux i’m always executing it in window but i’m using this script to change the resolution. of course the “-f” variable is loaded from setting.ini but it dont write it to the linux launcher…

example for 64bit launcher: (it’s dynamicaly created by the game on exit, settings is in game menu)

xrandr -s 1280x960&
/usr/share/biw-game/BiW64 -w  1280 960 -g show_framerate = 1 
xrandr -s 1280x1024 #this resolution is taken from the DesktopRes.ini it's using this command "xrandr  | grep \* | cut -d' ' -f4 >DesktopRes.ini"

i hope you understand this… it’s not so clear but it’s working with this tool added to the package

hey saga you have the 32bit ubuntu? if yes, can you please provide my the wmctrl 32bit binary? bcs i cant install it on 64bit system and i cant find it on the net… thank you…

edit: nevermind i have extracted it from the deb package :smiley: