0

Using Tkinter in Python 3.9, why does this raise bad geometry specifier "140mx120m"?

import tkinter as tk

window = tk.Tk()
window.geometry('140mx120m')

Without millimeter unit m it works fine, but I'd have to convert to pixels.

For instance this example and this one suggest it should work. What am I missing?

martineau
  • 119,623
  • 25
  • 170
  • 301
RolfBly
  • 3,612
  • 5
  • 32
  • 46
  • Do you know how to convert millimeters to screen pixels? Also, although it's irrelevant here (for other reasons), note that the common abbreviation for millimeters is `mm` not `m`. – martineau May 22 '21 at 19:24
  • OK, I understand the reason behind you using "m" instead of 'mm". However I have no idea why you wanted me to read [this](https://stackoverflow.com/help/behavior) since as far as I know none of which seemed relevant to this discussion or your question. – martineau May 23 '21 at 21:12
  • @martineau Why do you ask your first question? Html lets you specify screen distances in cm or pixels. I was expecting the same from tk geometry, but it doesn't. – RolfBly May 23 '21 at 21:36
  • I said I understand now why you were using "m". I originally asked you did you know how to to convert millimeters to **screen pixels**. Regardless, you haven't explained why you wanted me to read the article about [Expected Behavior](https://stackoverflow.com/help/behavior). Also how html does things is not relevant. – martineau May 23 '21 at 21:37
  • @martineau To find out screen dimensions programmatically, see [here](https://www.blog.pythonlibrary.org/2015/08/18/getting-your-screen-resolution-with-python/). I can make an educated guess at screen size in cm^2, for my application. Hence, I can compute pixels per unit distance. The way you ask comes across as 'you can't do that!', which I find rather condescending, which is why I recommended the link. Does that answer your questions? – RolfBly May 24 '21 at 11:16
  • Thanks, but I already know how to get the screen resolution programmatically. My question was not in anyway meant to be condescending, sorry you took it that way. I asked because I have figured-out how to calculate it programmatically at runtime (not guess based on overall screen dimensions) the pixels-per-inch (or dots-per-millimeter or any other units you like) are and was considering posting an answer to this question to show you how to set the geometry to something equivalent to what you want — even though that is not exactly the question you asked. – martineau May 24 '21 at 12:01
  • @martineau First of all, I'm sorry I misinterpreted your question. Second, how about posting the question + answer in a separate post? Given your rep I assume you already know [it's perfectly ok to do so](https://stackoverflow.com/help/self-answer). While it doesn't answer the question I asked, I think it would be helpful and it does indeed fit in this context. – RolfBly May 25 '21 at 13:29
  • RolfBly: Interesting suggestion, thanks. I know someone can answer their own question and have occasionally seen "leading" questions here that were posted simply so the author could post their own solution. However I generally don't care for them — the idea just seems too contrived to me (stackoverflow isn't meant to be some sort of recipes site). So for those reasons I may post an "answer" here showing how to accomplish something equivalent to what you where trying to do. Feel free to ignore it. – martineau May 26 '21 at 15:10

1 Answers1

1

AFAIK, tkinter has never supported geometries measured in millimeters.

From the official tcl/tk documentation:

NewGeometry has the form =widthxheight±x±y, where any of =, widthxheight, or ±x±y may be omitted. Width and height are positive integers specifying the desired dimensions of window... X and y specify the desired location of window on the screen, in pixels.

Nowhere does it say you can use screen distances rather than pixels. The documentation is virtually unchanged even if you go all the way back to tcl/tk 8.0.

Screen distances (as mentioned in the first link in your question) can be used in other places, such as specifying the width and height of widgets. The geometry method doesn't use screen distances.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • According to the [tk scaling documentation](http://tcl.tk/man/tcl8.5/TkCmd/tk.htm#M7) you can call it without specifying a scaling factor argument to retrieve its current value — I just tried and it worked. This implies it would possible to calculate what pixel values to use with in a `geometry()` call to obtain the desired size and/or position. – martineau May 15 '21 at 10:18
  • @martineau: yes, of course. You can calculate a size in pixels based on millimeters and the screen scaling factor, but the question was about whether the `geometry` command will accept millimeters. – Bryan Oakley May 15 '21 at 14:13
  • Yes, I realize that — my point was only that it's possible to achieve the same effect by obtaining and using the information `tk scaling` will return (as a work-around). – martineau May 15 '21 at 14:29