wx.StaticText's label does not contract using SetSizerAndFit()

Question:

I’d like to place a filename after the file is chosen and contract it if the file name is too long. Here is my code:

import wx

class Dlg(wx.Dialog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.input_pnl = wx.Panel(self)
        self.choose_input = wx.Button(self.input_pnl, label='Select')
        self.input_filename = wx.StaticText(self.input_pnl, style=wx.ST_ELLIPSIZE_END)
        self.input_szr = wx.BoxSizer(wx.HORIZONTAL)
        self.input_szr.AddMany([
            (wx.StaticText(self.input_pnl, label='File to analyze'), 0, wx.ALIGN_CENTER),
            (self.choose_input, 0, wx.LEFT|wx.RIGHT, 7),
            (self.input_filename, 0, wx.ALIGN_CENTER)
        ])
        self.input_pnl.SetSizer(self.input_szr)

        borderSizer = wx.BoxSizer(wx.VERTICAL)
        borderSizer.AddMany([
            (self.input_pnl, 0, wx.EXPAND|wx.ALL, 10),
            (wx.StaticText(self, label='Some example text to shift the border to the right'), 0, wx.ALL, 10)])
        self.SetSizerAndFit(borderSizer)

        self.choose_input.Bind(wx.EVT_BUTTON, self.on_choose_input)
    
    def on_choose_input(self, event):
        with wx.FileDialog(self, "Open", wildcard="Plain text (*.txt)|*.txt",
                    style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            self.input_pathname = fileDialog.GetPath()
            self.input_filename.SetLabel(fileDialog.GetFilename())

if __name__ == "__main__":
    app = wx.App()
    dlg = Dlg(None)
    dlg.ShowModal()
    dlg.Destroy()
    app.MainLoop()

But the label is not contracted normally, supposedly, because of the SetSizerAndFit() method.

Asked By: Deeproad

||

Answers:

Calling Layout, once you’ve selected the filename works, on Linux at least.

You might also consider adding wx.RESIZE_BORDER to the dialog, so that the dialog is re-sizeable, as a last resort.

import wx

class Dlg(wx.Dialog):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.input_pnl = wx.Panel(self)
        self.choose_input = wx.Button(self.input_pnl, label='Select')
        self.input_filename = wx.StaticText(self.input_pnl, style=wx.ST_ELLIPSIZE_END)
        self.input_szr = wx.BoxSizer(wx.HORIZONTAL)
        self.input_szr.AddMany([
            (wx.StaticText(self.input_pnl, label='File to analyze'), 0, wx.ALIGN_CENTER),
            (self.choose_input, 0, wx.LEFT|wx.RIGHT, 7),
            (self.input_filename, 0, wx.ALIGN_CENTER)
        ])
        self.input_pnl.SetSizer(self.input_szr)

        borderSizer = wx.BoxSizer(wx.VERTICAL)
        borderSizer.AddMany([
            (self.input_pnl, 0, wx.EXPAND|wx.ALL, 10),
            (wx.StaticText(self, label='Some example text to shift the border to the right'), 0, wx.ALL, 10)])
        self.SetSizerAndFit(borderSizer)

        self.choose_input.Bind(wx.EVT_BUTTON, self.on_choose_input)

    def on_choose_input(self, event):
        with wx.FileDialog(self, "Open", wildcard="Plain text (*.txt)|*.txt",
                    style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:

            if fileDialog.ShowModal() == wx.ID_CANCEL:
                return

            self.input_pathname = fileDialog.GetPath()
            self.input_filename.SetLabel(fileDialog.GetFilename())
            self.Layout()

if __name__ == "__main__":
    app = wx.App()
    dlg = Dlg(None, style=wx.RESIZE_BORDER)
    dlg.ShowModal()
    dlg.Destroy()
    app.MainLoop()

enter image description here

Answered By: Rolf of Saxony
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.