Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

python - Show another window wxpython?

I have been looking around the Internet but I am not sure if there is a way to show 2 classes in wxPython in 2 separate windows. And could we communicate between them (like one class being the dialog and the other the main class)?

I think I did this before using Show() but I am not sure how to repeat this.

So basically I would like to be able to have a dialog but by using a class instead. This would be more powerful than using Modal dialogs.

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here you have a simple example of two frames communicating:

enter image description here

The trick is in sending an object reference to share between frames, either creating one inside the other (as in this case) or through a common parent. The code is:

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, size=(150,100), title='MainFrame')
        pan =wx.Panel(self)
        self.txt = wx.TextCtrl(pan, -1, pos=(0,0), size=(100,20), style=wx.DEFAULT)
        self.but = wx.Button(pan,-1, pos=(10,30), label='Tell child')
        self.Bind(wx.EVT_BUTTON, self.onbutton, self.but)
        self.child = ChildFrame(self)
        self.child.Show()

    def onbutton(self, evt):
        text = self.txt.GetValue()
        self.child.txt.write('Parent says: %s' %text)


class ChildFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, None, size=(150,100), title='ChildFrame')
        self.parent = parent
        pan = wx.Panel(self)
        self.txt = wx.TextCtrl(pan, -1, pos=(0,0), size=(100,20), style=wx.DEFAULT)
        self.but = wx.Button(pan,-1, pos=(10,30), label='Tell parent')
        self.Bind(wx.EVT_BUTTON, self.onbutton, self.but)

    def onbutton(self, evt):
        text = self.txt.GetValue()
        self.parent.txt.write('Child says: %s' %text)


if __name__ == "__main__":

    App=wx.PySimpleApp()
    MainFrame().Show()
    App.MainLoop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...