如何在子对话框执行某些操作时刷新主框架

时间:2017-09-14 09:23:16

标签: python-3.x refresh

我希望主框架刷新更新标签当我点击"完成" MyDialog上的按钮,但现在它无法正常工作。

有什么不对吗?感谢。

这是代码: MyDialog:子对话框,其上有一个按钮,用于更新主框架的标签 MainFrame:主框架,它上面有一个按钮来启动我的对话框

# -*- coding: utf-8 -*-

import wx

#Dialog
class MyDialog(wx.Dialog):
    """setting MyDialog."""
    def __init__(self):
        self.dlg_main = wx.Dialog.__init__(self, None, -1, title="setting", size=(300, 300))
        self.btn_ok = wx.Button(self, label="done", pos=(30, 30), size=(50, 26))
        self.Bind(wx.EVT_BUTTON, self.__OnButtonClick_save, self.btn_ok,)

    def __OnButtonClick_save(self, event):
        self.Destroy()
        main = MainFrame()
        **main.set_label_name('test')**
        main.Destroy()

def start_dialog():
    my_dialog = MyDialog()
    my_dialog.ShowModal()
    my_dialog.Destroy()

#Main Frame
class MainFrame(wx.Frame):
    def __init__(self):
        self.main_frame = wx.Frame.__init__(self, None, -1, title='simple', size=(400, 400))
        self.Centre()
        self.label_name = wx.StaticText(self, label="Hello,everyone", pos=(30, 30))
        self.btn_set = wx.Button(self, label="set", pos=(30, 60))
        self.Bind(wx.EVT_BUTTON, self.on_button_click, self.btn_set)

    def set_label_name(self, str):
        print(str)
        self.label_name.SetLabel('hello, Boys')

    def on_button_click(self, event):
        start_dialog()

def show_main():
    main = wx.App()
    main_win = MainFrame()
    main_win.Show()
    main.MainLoop()

if __name__ == '__main__':
    show_main()

1 个答案:

答案 0 :(得分:0)

我有方法,谢谢大家!

关键是如何调用父方法,因此,使用self.parent.xxx来解决问题。

这样的代码:

# -*- coding: utf-8 -*-

import wx

#Dialog
class MyDialog(wx.Dialog):
    """setting MyDialog."""
    def __init__(self, parent, title):
        super(MyDialog, self).__init__(parent, title=title, size=(300, 300))
        self.parent = parent
        panel = wx.Panel(self)
        btn_ok = wx.Button(panel, label="done", pos=(30, 30), size=(50, 26))
        btn_ok.Bind(wx.EVT_BUTTON, self.__OnButtonClick_save)

    def __OnButtonClick_save(self, event):
        #THis is the different
        self.parent.set_label_name('test')
        self.Destroy()

def start_dialog():
    my_dialog = MyDialog()
    my_dialog.ShowModal()
    my_dialog.Destroy()

#Main Frame
class MainFrame(wx.Frame):
    def __init__(self):
        self.main_frame = wx.Frame.__init__(self, None, -1, title='simple', size=(400, 400))
        self.init_ui()

    def init_ui(self):
        self.label_name = wx.StaticText(self, label="Hello,everyone", pos=(30, 30))
        btn_set = wx.Button(self, label="set", pos=(30, 60))
        btn_set.Bind(wx.EVT_BUTTON, self.on_button_click)
        self.Centre()

    def set_label_name(self, str):
        self.label_name.SetLabel('hello, Boys')

    def on_button_click(self, event):
        my_dialog = MyDialog(self, "setting")
        my_dialog.ShowModal()
        my_dialog.Destroy()

def show_main():
    main = wx.App()
    main_win = MainFrame()
    main_win.Show()
    main.MainLoop()

if __name__ == '__main__':
    show_main()

感谢您在此回答中提出的想法Wxpython show dialog on main frame startup