Tuesday, 15 September 2020

How to obtain widget/font size with different DPI and Resolutions in wxPython?

I have written a programm with wxPython. We run that programm on many different computer with different Display configurations.

Examples:

  • 1920x1080, DPI 100%

  • 3000x2000, DPI 175%

  • 1980x1080, DPI 150% (small 13" Display Laptop)

I code on an Full HD, DPI 100% display - the GUI looks fine! Especially on the 3000x2000 i have the Problem that TextCtrls, BitmapButtons and Images in general change their size (they get much smaller) and the GUI is messed up. Same for the DPI 150%.

I tried to prevent it by setting the DPI awareness of my process in my Main Class. But maybe i am doing something wrong or there is a lot more i have to take care of. Also this is no consistent solution for me.

Here is the Code i used to set DPI Awareness:

import ctypes

# Query DPI Awareness (Windows 10 and 8)
awareness = ctypes.c_int()
errorCode = ctypes.windll.shcore.GetProcessDpiAwareness(0, ctypes.byref(awareness))

# Set DPI Awareness  (Windows 10 and 8)
errorCode = ctypes.windll.shcore.SetProcessDpiAwareness(2)

Question:

  1. What do i need to do to make my GUI look at least similar on all DPI and Resolution setting?
  2. Is there anything i need to take care of when using images?
  3. Do i need to scale widget/font size in my code directly?
  4. Are there any tips with setting the size and distance of widgets in sizers?

I will attach a code example where I try to do as much size and positioning stuff as possible to give you guys a base to maybe fix somthing.

import wx import wx.adv

class Mywin(wx.Frame):
    def __init__(self,parent,title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title,size= (600,-1))
        
        self.SetSize(800,500)

        sizer_1 = wx.BoxSizer(wx.VERTICAL)        
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Order ID'), 0, wx.ALL , 5)
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Status'), 0, wx.ALL , 5)
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Creator'), 0, wx.ALL , 5)
        sizer_1.Add((-1, 5))
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Comment'), 0, wx.ALL , 5)
        sizer_1.Add((-1, 40))
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Customer ID'), 0, wx.ALL , 5)
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Customer Name'), 0, wx.ALL , 5)        
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Contact Person'), 0, wx.ALL , 5)
        sizer_1.Add((-1, 21))
        sizer_1.Add(wx.StaticText(self, size=(125,-1), label='Inquiry Date'), 0, wx.ALL , 5)


        ctrl_height = 25
        stati = ["Test", "Example"]
        
        tc_order_id = wx.TextCtrl(self, -1, size=(100, ctrl_height),style=wx.TE_PROCESS_ENTER)
        cb_status = wx.ComboBox(self,size=(100, ctrl_height), choices=stati, style=wx.CB_READONLY)        
        cb_creator = wx.ComboBox(self,size=(100, ctrl_height), choices=stati, style=wx.CB_READONLY)
        tc_comment = wx.TextCtrl(self, id=-1, value='', size=(300,55), style=wx.TE_MULTILINE|wx.SUNKEN_BORDER)        
        tc_customer_id = wx.TextCtrl(self, -1, size=(80, ctrl_height),style=wx.TE_PROCESS_ENTER)                             
        tc_customer_name = wx.TextCtrl(self, -1, size=(300, ctrl_height),style=wx.TE_READONLY)        
        tc_contact_id = wx.TextCtrl(self, -1, size=(80, ctrl_height),style=wx.TE_PROCESS_ENTER)
        
        calender_style = wx.adv.DP_DROPDOWN | wx.adv.DP_SHOWCENTURY | wx.adv.DP_ALLOWNONE
        tc_order_date = wx.adv.GenericDatePickerCtrl(self, size=(90,ctrl_height), style = calender_style)
        
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        sizer_2.Add(tc_order_id, 0)
        sizer_2.Add(cb_status,0, wx.TOP , 3)
        sizer_2.Add(cb_creator, wx.TOP , 2)
        sizer_2.Add(tc_comment, 0, wx.TOP, 9)
        sizer_2.Add(tc_customer_id, 0, wx.TOP, 4)
        sizer_2.Add(tc_customer_name, 0, wx.TOP, 4)
        sizer_2.Add(tc_contact_id, 0, wx.TOP, 4)
        sizer_2.Add(tc_order_date, 0, wx.TOP, 20)
        
        mainsizer = wx.BoxSizer(wx.HORIZONTAL)
        mainsizer.Add(sizer_1)
        mainsizer.Add(sizer_2)
        
        self.SetSizer(mainsizer)
        
        self.Centre()
        self.Show()
        self.Layout()


demo = wx.App()
Mywin(None,'Example Code')
demo.MainLoop()


from How to obtain widget/font size with different DPI and Resolutions in wxPython?

No comments:

Post a Comment