I have a wxPython application that remembers its windows positions and sizes by writing them down into a configuration file upon closing, and restoring them from the config file upon launch. Worked without a problem on Windows for many years. Recently I had to use that on Linux. I’ve tried on the following two configurations:
#1 MX Linux KDE
Python 3.11.2
wx.__version__
'4.2.0'
KDE Plasma 5.27.5
Kernel 6.1.0-17-amd64
X11
#2 Fedora 40 KDE
Python 3.12.3
wx.__version__
4.2.1
KDE Plasma 6.0.5
Kernel 6.8.10-300.fc40.x86_64
Wayland
On MX Linux it worked more or less the same as on Windows. On Fedora, however, I could only get or set the width and height of the window, but not its top left corner position: GetPosition()
and GetScreenPosition()
always return (0, 0)
, GetRect()
always return (0, 0, width, height)
. SetPosition()
or SetRect()
have no visible effect regarding the window position, although SetRect()
sets the width and height correctly. What gives? Some applications on Fedora 40 seem to be able to remember and restore their position (Skype, for example), so it’s probably not a system limitation. But how do I do that for my own wxPython application?
Here’s the minimal example to reproduce the behavior I’m talking about. I’ve also noticed that EVT_MOVE
message is not handled on Fedora, which may be related to the issue (not included into this example).
#!/usr/bin/env python3
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, window_id, title):
wx.Frame.__init__(self, parent, window_id, title)
self.Bind(wx.EVT_CLOSE, self.OnClose)
self.Show(True)
def ReadSettings(self):
rect = [0, 0, 100, 100]
try:
with open("settings.txt", "r") as f:
rect = [int(s) for s in f.read().split(",")]
except:
pass
self.SetSize(*rect)
def WriteSettings(self):
with open("settings.txt", "w") as f:
f.write(",".join((str(s) for s in self.GetRect())))
def OnClose(self, evt):
self.WriteSettings()
self.Destroy()
app = wx.App(0)
frame = MainWindow(None, -1, "Test")
frame.ReadSettings()
frame.Show(1)
app.MainLoop()