Transferring values between forms (windows) in wxPython

Asked 2 years ago, Updated 2 years ago, 16 views

"We are creating a program that changes the form (window) with ""Next"" and ""Back"" on wxPython.
I would like to refer to the value specified in the form from which I moved.How should I do this?

The things I want to do are as follows.

From form

def button_modify_click(self, event):
    dlg = frmModify.frmModify()
    global room_num
    room_num=u"104"
    dlg.Show()
    self.Close (True)
    return True

Destination form

class frmModify (wx.Frame):
    def__init__(self):
        global room_num
        (omitted)
        self.text_room_no=wx.TextCtrl(pRoom, wx.ID_ANY, room_num, size=(640,64))

Thank you for your kind cooperation.

python

2022-09-30 17:54

1 Answers

How about adding properties to the destination form, setting them at the destination, and then showing them?
If you want to reflect value updates between windows, you can also bind them.

Below is a sample code that passes the values in the function.

import wx

class frmModify (wx.Frame):
    def__init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Transition Destination")
        self.SetSize (680,80))
        pRoom=wx.Panel(self)
        
        self.text_room_no=wx.TextCtrl(pRoom,wx.ID_ANY,size=(640,64))

    # a function that passes a value
    def ImportRoomNum(self, room_num):
        Sent to self.text_room_no.SetValue(room_num+"")

class MyFrame (wx.Frame):
    def__init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        self.SetSize((60,120))
        panel=wx.Panel(self)
        
        self.room_name = "Room 5"
        
        self.text_room=wx.TextCtrl(panel, wx.ID_ANY, self.room_name, pos=(10,40), size=(100,25))
        self.button_modify=wx.Button(panel, label="modify", pos=(10,10), size=(100,25))
        self.Bind (wx.EVT_BUTTON, self.button_modify_click, self.button_modify)
        
        self.Show (True)

    def button_modify_click(self, event):
        dlg = frmModify()
        dlg.ImportRoomNum(self.text_room.GetValue())#Transfer value between forms (windows)
        dlg.Show()
    
    def close (self, event):
        self.Close (True)

    def printer (self, event):
        print("Button2")
        
app=wx.App()
frame = MyFrame (None, wx.ID_ANY, "Transition source")
app.MainLoop()

This post is based on @MaFengLing's Comments and so on. Community Wiki.

This post is based on @MaFengLing's Comments and so on. Community WikiPosted as


2022-09-30 17:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.