Python Forum
tkinter canvas; different page sizes on different platforms?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter canvas; different page sizes on different platforms?
#1
Thumbs Up 
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!
Reply
#2
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.
philipbergwerf likes this post
Reply
#3
(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 :)
Reply
#4
(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?
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PY QT APP]HOW TO uniform build and distribution for different platforms djibb 1 637 Jul-11-2023, 01:46 AM
Last Post: Larz60+
  tkinter.TclError: can't invoke "canvas" command cybertooth 8 5,961 Feb-23-2023, 06:58 PM
Last Post: deanhystad
  how to add two numbers and pass the result to the next page in tkinter? pymn 7 4,338 Feb-15-2022, 04:40 AM
Last Post: pymn
  [Tkinter] Clickable Rectangles Tkinter Canvas MrTim 4 8,849 May-11-2021, 10:01 PM
Last Post: MrTim
  [Tkinter] Draw a grid of Tkinter Canvas Rectangles MrTim 5 7,899 May-09-2021, 01:48 PM
Last Post: joe_momma
  how to resize image in canvas tkinter samuelmv30 2 17,747 Feb-06-2021, 03:35 PM
Last Post: joe_momma
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 4,567 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  how to rotate lines in tkinter canvas helpmewithpython 1 3,415 Oct-06-2020, 06:56 PM
Last Post: deanhystad
  Tkinter function to clear old canvas and start a fresh "operation" zazas321 5 9,435 Oct-01-2020, 04:16 AM
Last Post: zazas321
  question on tkinter canvas PhotoImage gr3yali3n 1 2,125 Sep-05-2020, 12:18 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020