Python Forum

Full Version: tkinter canvas; different page sizes on different platforms?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am working on an alternative music notation app for a long time and I have a problem. I use Tkinter canvas for drawing the whole score. I get different results on:
paperheigth = root.winfo_fpixels('1m') * 297 * (scale_S)  # a4 210x297 mm
paperwidth = root.winfo_fpixels('1m') * 210 * (scale_S)
I use this to get the size of the paper. The problem is that the outcoming number is different on every platform so the score looks different too. How can I create a standard a4 format that is the same on windows, mac and linux? I tried to use the same numbers but the problem stays the same. I hope someone knows a solution... to make it concrete:
from tkinter import Tk, Canvas

root = Tk()
canvas = Canvas(root)
canvas.place(relwidth=1, relheight=1)

paperheigth = root.winfo_fpixels('1m') * 297
paperwidth = root.winfo_fpixels('1m') * 210
canvas.create_rectangle(20, 20, 20+paperwidth, 20+paperheigth, outline='', fill='white')
canvas.create_line(100, 40, 100, 700, width=2)

root.mainloop()
I want the paper and the line look exactly the same on all different platforms. How to do this?
Thank you!
The number of vertical and horizontal pixels for your A4 page is going to change based on the screen resolution. In other words, the numbers should change. If you want your output to look the same on different screens you will have to scale your drawing to compensate. Your page and your notes need to be different numbers of pixels to appear the same, and "the same" is only going to be "approximately the same".

It looks like you are using tkinter. I think I would subclass the Canvas widget and add methods for:
create_rectangle_mm(), create_line_mm(), etc... These methods take the mm input and convert to pixels using a dots per mm conversion factor. You would do all your drawing using mm units instead of pixels. Something like this was probably done already. It would be worth searching for a drawing package that has this already built in.

Any images, for notes, clef, etc would also need to be rescaled too.
(Mar-25-2021, 10:06 PM)deanhystad Wrote: [ -> ]The number of vertical and horizontal pixels for your A4 page is going to change based on the screen resolution. In other words, the numbers should change. If you want your output to look the same on different screens you will have to scale your drawing to compensate. Your page and your notes need to be different numbers of pixels to appear the same, and "the same" is only going to be "approximately the same".

It looks like you are using tkinter. I think I would subclass the Canvas widget and add methods for:
create_rectangle_mm(), create_line_mm(), etc... These methods take the mm input and convert to pixels using a dots per mm conversion factor. You would do all your drawing using mm units instead of pixels. Something like this was probably done already. It would be worth searching for a drawing package that has this already built in.

Any images, for notes, clef, etc would also need to be rescaled too.
Thank you for this explanation! I understand the problem now and will post my solution in this post as soon as I solved it :)
(Mar-25-2021, 10:06 PM)deanhystad Wrote: [ -> ]The number of vertical and horizontal pixels for your A4 page is going to change based on the screen resolution. In other words, the numbers should change. If you want your output to look the same on different screens you will have to scale your drawing to compensate. Your page and your notes need to be different numbers of pixels to appear the same, and "the same" is only going to be "approximately the same".

It looks like you are using tkinter. I think I would subclass the Canvas widget and add methods for:
create_rectangle_mm(), create_line_mm(), etc... These methods take the mm input and convert to pixels using a dots per mm conversion factor. You would do all your drawing using mm units instead of pixels. Something like this was probably done already. It would be worth searching for a drawing package that has this already built in.

Any images, for notes, clef, etc would also need to be rescaled too.
So the measure unit from tkinter differs from computer to computer?
The measure unit in Canvas is pixels. It changes from display to display. This is very common for graphics. This is why I got a 27" monitor to work from home. My 13" laptop screen is ok for emails, but for writing code I like the font to be larger while still seeing the same number of lines. The number of pixels is nearly the same for the two displays, but the screen size is about 3.5 times larger. Wanting software to look the same large and small screens is not the norm.

There is something tricky in Canvas. You can rescale shapes added to a canvas. Theoretically you could draw everything first using pixel dimensions, then rescale the drawing to adjust for pixel density. But this only works for lines and shapes. Images are not resized.

You will also want to know screen size in mm. Look here for a mention on how to do that.

https://python-forum.io/Thread-Tkinter-guide?page=2