0

I'm getting a different screen resolution (in a Python script) depending on whether I type commands into Cygwin manually or run them using a shell script, and I can't understand why.

I'm running Windows 8.1 / Cygwin 2.8.2 (and XWin Server at startup) / Python 2.7.13 (and am new to Cygwin and Python; please let me know if I can provide more details).

When I start Windows, open a Cygwin terminal, enter export DISPLAY=:0.0 and then enter python c:/users/<my directories>/<python-file>.py, my GUI Python script runs normally.

(If w, h = root.winfo_screenwidth(), root.winfo_screenheight() appears in the Python script, for example, then the outputted values, corresponding to the screen resolution, are (1920, 1200).)

However, if I try to automate this process by double-clicking on the following Windows batch file:

c:\cygwin\bin\bash c:/users/<my directories>/<shell-file>.sh

where the shell file contains

#!/bin/sh
export DISPLAY=:0.0
python c:/users/<my directories>/<python-file>.py

then my GUI Python script runs but is distorted (i.e., it's too large for the screen), and the resolution comes back as (1280, 800).

Why do these two methods give different results? How might I get a Python script to run with a resolution of (1920, 1200) in an automated way? Thank you.

2

I don't know exactly what the issue is but here are a couple of things:

Environment

My first guess would be a difference in the environment when you use the cygwin terminal and when you execute bash through the batch file.

When running from the cygwin terminal execute the command env to see what the environment is (it's been a while since I've used windows, but I suspect env is part of cygwin or built into bash).

Then add the same command to the start of your shell script.

#!/bin/sh
export DISPLAY=:0.0
env
python c:/users/<my directories>/<python-file>.py

Compare the two environments and see if there are any differences.

pro-tip: you can write the output of the env command to a file with env > c:/users/<your directories>/env1.txt (then use env2.txt from your shell script) and then compare them with the command line tool diff. i.e. diff env1.txt env2.txt.

Interpreter

The next thing is related, but what python interpreter is being executed in each case. Do you only have one python installed on your system? Or might you have multiple. For example did you install through both cygwin and anaconda?

If you have multiple, then check which version is being executed with which python in each case.

GUI Toolkit

What gui toolkit are you using. I'm not familiar with the API winfo_screenwidth(). Presumably this is provided by some library/package you are using. Which package is that?

I'm not sure about the windows world/cygwin, but in the linux world it is possible for a program to determine whether or not stdin is a terminal. The toolkit you are using might have different behavior in either case. If so, the toolkit documentation would be where to go for that info.

In addition, IIRC there are two ways of starting a program in windows. There's the standard way i.e. int main() and the GUI way (I don't recall the name of the main function in this case). I recall that for some interpreted languages there are different interpreter binaries depending on which entry point you wanted. The gui one might start or end with a w. For instance I think javaw.exe is the gui version and java.exe is the console version (for java). Is there an equivalent for python in windows?

  • Thank you @cheshirekow! env wouldn't even work in whatever environment my bash script was operating in, indicating that I was far from the Cygwin environment that I was launching manually. Adding set PATH=c:\cygwin\bin;%PATH% to the batch file, based on this answer, was enough to fix the problem – Chemomechanics Sep 7 '17 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.