Wxpython : is there a way to exit function when another function is called?

I have two functions. i want to exit the first one when the second one is running.
code :slight_smile:

import wx

class MyFrame(wx.Frame):
def init(self):
super().init(parent=None, title=‘Simple App’)
panel = wx.Panel(self)

    def OnInput(event):

        #static text
        text = wx.StaticText(panel, label ="Type your text", pos =(50, 100))

        #input text
        type = wx.TextCtrl(panel, pos = (50, 150))

    def OnReadonly(event):

        #static text
        text = wx.StaticText(panel, label ="Status", pos =(50, 100))

        #input text
        input = wx.TextCtrl(panel, pos = (50, 150), style=wx.TE_MULTILINE|wx.TE_READONLY)
        input.SetValue("Waiting for your file")

    menu = wx.Menu()
    it = menu.Append(-1, "Input Text")
    menu.AppendSeparator()
    rt = menu.Append(-1, "Readonly Text")
    self.Bind(wx.EVT_MENU, OnInput, it)
    self.Bind(wx.EVT_MENU, OnReadonly, rt)
    menuBar = wx.MenuBar()
    menuBar.Append(menu, "Simple Menu")
    self.SetMenuBar(menuBar)

    self.Show()

if name == ‘main’:
app = wx.App()
frame = MyFrame()
app.MainLoop()

Hi and welcome to Discuss wxPython,

Technically, both the functions do exit automatically as there is nothing in their code that prevents them from returning.

I guess what you really mean is that you want each function to undo the effects of the other function?

In their current form the functions will keep creating new controls each time they are called and you will end up will multiple controls on top of each other, which is not a good idea.

Instead of creating the controls in the functions, you could initially create and hide all the controls in the __init__ method and then use the OnInput and OnReadonly methods to set the contents of the controls and show/hide the appropriate ones for the required mode.

Here is a simple example:

import wx

class MyFrame(wx.Frame):
    def __init__(self):
        super().__init__(parent=None, title='Simple App')
        self.panel = wx.Panel(self)

        menu = wx.Menu()
        it = menu.Append(-1, "Input Text")
        menu.AppendSeparator()
        rt = menu.Append(-1, "Readonly Text")
        self.Bind(wx.EVT_MENU, self.OnInput, it)
        self.Bind(wx.EVT_MENU, self.OnReadonly, rt)
        menuBar = wx.MenuBar()
        menuBar.Append(menu, "Simple Menu")
        self.SetMenuBar(menuBar)

        self.static_text = wx.StaticText(self.panel, label="", pos=(50, 100))
        self.static_text.Hide()

        self.input_text_ctrl = wx.TextCtrl(self.panel, pos=(50, 150))
        self.input_text_ctrl.Hide()

        self.readonly_text_ctrl = wx.TextCtrl(self.panel, pos=(50, 150), style=wx.TE_MULTILINE | wx.TE_READONLY)
        self.readonly_text_ctrl.Hide()

        self.Show()

    def OnInput(self, _event):
        self.static_text.SetLabel("Type your text")
        self.static_text.Show()
        self.readonly_text_ctrl.Hide()
        self.input_text_ctrl.Show()
        self.input_text_ctrl.SetValue('')

    def OnReadonly(self, _event):
        self.static_text.SetLabel("Status")
        self.static_text.Show()
        self.input_text_ctrl.Hide()
        self.readonly_text_ctrl.Show()
        self.readonly_text_ctrl.SetValue("Waiting for your file")


if __name__ == '__main__':
    app = wx.App()
    frame = MyFrame()
    app.MainLoop()

Tested using Python 3.10.12 + wxPython 4.2.1 gtk3 (phoenix) wxWidgets 3.2.2.1 on Linux Mint 21.2

1 Like

hello, thank you for your response. that exactly what am looking for. greetings