显示另一个窗口 wxpython? [英] Show another window wxpython?
问题描述
我一直在浏览互联网,但我不确定是否有办法在 2 个单独的窗口中显示 wxPython 中的 2 个类.我们可以在它们之间进行交流吗(比如一个类是对话框,另一个类是主类)?
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)?
我想我是在使用 Show()
之前这样做的,但我不知道如何重复这个.
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.
谢谢
推荐答案
这里有一个简单的两帧通信示例:
Here you have a simple example of two frames communicating:
诀窍是发送一个对象引用以在帧之间共享,要么在另一个内部创建一个(如本例中),要么通过一个共同的父级.代码是:
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()
这篇关于显示另一个窗口 wxpython?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!