wxpython:打开对话框时如何访问窗口

时间:2018-10-31 18:15:32

标签: dialog wxpython accessibility

我了解到ShowModal()时对话框的父级应该不可访问。但是,需要我的程序才能访问单独的窗口,以便用户可以查看该窗口上的信息,以确定在对话框上单击哪个按钮。像下面的程序一样,当“对话框”打开时,我希望能够访问“不同的框架”。我该怎么做?

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Different Frame", size=(300, 300))
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        btn = wx.Button(panel, -1, "open frame")
        self.Bind(wx.EVT_BUTTON, self.OnOpenFrame, id=btn.GetId())
        sizer.Add(btn)
        text = "This is a line.\n" * 100
        txtCtrl = wx.TextCtrl(panel, -1, text, style=wx.TE_MULTILINE,
                              size=(200,200))
        sizer.Add(txtCtrl)
        panel.SetSizer(sizer)
        self.Centre()
        self.Show()

    def OnOpenFrame(self, evt):
        ParentFrame()

class ParentFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title="Parent Frame",size=(500, 500))
        panel = wx.Panel(self)
        btn = wx.Button(panel, -1, "open dialog")
        self.Bind(wx.EVT_BUTTON, self.OnOpenDialog, id=btn.GetId())
        self.Centre()
        self.Show()

    def OnOpenDialog(self, event):
        dlg1 = MyDialog(self)
        val = dlg1.ShowModal()

        dlg2 = MyDialog(self, val)
        dlg2.ShowModal()

class MyDialog(wx.Dialog): 
    def __init__(self, parent, prevAnsw=None): 
        wx.Dialog.__init__(self, parent, -1, title = "Dialog") 
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        if prevAnsw is not None:
            sizer.Add(wx.StaticText(
                panel, -1, "Answer from previous Dialog: %s" % prevAnsw))
        sizer.Add(wx.StaticText(panel, -1, "Try to scroll in Different Frame to "
                                "see if it is blocked"))
        btn1 = wx.Button(panel, -1, "Pass 1")
        self.Bind(wx.EVT_BUTTON, self.OnPass1, id=btn1.GetId())
        sizer.Add(btn1)

        btn2 = wx.Button(panel, -1, "Pass 2")
        self.Bind(wx.EVT_BUTTON, self.OnPass2, id=btn2.GetId())
        sizer.Add(btn2)
        panel.SetSizer(sizer)

    def OnPass1(self, evt):
        self.EndModal(1)

    def OnPass2(self, evt):
        self.EndModal(2)

app = wx.App(0)
frame = MyFrame(None)
frame.Show()
app.MainLoop()

2 个答案:

答案 0 :(得分:1)

简单的答案是不要将其设为dialog
下面是一个相当微不足道的伪对话,因为它只有在采取行动后才会返回。它通过在等待事件发生的Yield循环中调用while来保持访问其他窗口的能力。
由于其他窗口“处于活动状态”,因此激活该进程的按钮必须在每个进程完成后禁用并重新启用,因此这确实有点麻烦,但它应该为您指明实现目标的方向你想要的
也许可以将它本身写成ChainDialog模块,但我认为它很可能只是复制Mike Driscoll提到的Wizard模块。

import wx
import time

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Reference Frame", size=(300, 300))
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        self.btn = wx.Button(panel, -1, "open frame")
        self.Bind(wx.EVT_BUTTON, self.OnOpenFrame, id=self.btn.GetId())
        sizer.Add(self.btn)
        text = "This is a line.\n" * 100
        txtCtrl = wx.TextCtrl(panel, -1, text, style=wx.TE_MULTILINE,
                              size=(200,200))
        sizer.Add(txtCtrl)
        panel.SetSizer(sizer)
        self.Centre()
        self.Show()

    def OnOpenFrame(self, evt):
        DialogRequest(self)

class DialogRequest(wx.Frame):
    def __init__(self,parent):
        wx.Frame.__init__(self, parent, -1, title="Dialog Request Frame",size=(500, 500))
        self.parent = parent
        #Disable the "open frame" button
        self.parent.btn.Enable(False)

        #Use a dictionary to store chained results
        #Allows for rewriting of results if "Previous" button used
        self.stored_results = {}

        self.dlg_chain_no = 0
        self.dlg_prev_value = 0
        panel = wx.Panel(self)
        self.btn = wx.Button(panel, -1, "open dialog")
        self.Bind(wx.EVT_BUTTON, self.OnOpenDialog, id=self.btn.GetId())
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        self.Centre()
        self.Show()

    def OnOpenDialog(self, event):
        self.btn.Enable(False) #Disable open dialog

        #For test purposes there will be 4 chained dialogs
        #result -1 is a cancelled dialog
        #
        while self.dlg_chain_no != 4 and self.dlg_chain_no != -1:
            dlg = ChainDialog(self, self.dlg_chain_no)
            result = ChainDialog.Action(dlg)
            if result.chain_no == -1: # dialog Cancelled zero down stored values
                self.dlg_chain_no = 0
                self.stored_results = {}
                print("Dialog chain Cancelled")
                break
            self.dlg_chain_no = result.chain_no
            self.stored_results[result.chain_no] = result.result
            self.dlg_prev_value = result.result

        self.dlg_chain_no = 0
        self.dlg_prev_value = 0
        print("dialog values", self.stored_results)
        self.stored_results = {}

        self.btn.Enable(True) #Re-enable open dialog

    def OnClose(self, event):
        #Re-enable the "open frame" button
        self.parent.btn.Enable(True)
        event.Skip()

class ChainDialog(wx.Frame):
    def __init__(self, parent, chain_no):
        wx.Frame.__init__(self, parent, -1, title = "Dialog "+str(chain_no))
        self.chain_no = self.saved_chain_no = chain_no
        self.result = None
        self.parent = parent
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        if parent.dlg_prev_value != 0:
            sizer.Add(wx.StaticText(
                panel, -1, "Answer from previous Dialog: %s" % parent.dlg_prev_value))
        sizer.Add(wx.StaticText(panel, -1, "Try to scroll in Different Frame to "
                                "see if it is blocked"))
        btn1 = wx.Button(panel, -1, "Step "+str(self.chain_no))
        btn2 = wx.Button(panel, -1, "Previous")
        btn3 = wx.Button(panel, -1, "Cancel")
        self.Bind(wx.EVT_BUTTON, self.OnStep, id=btn1.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnPrev, id=btn2.GetId())
        self.Bind(wx.EVT_BUTTON, self.OnCancel, id=btn3.GetId())
        sizer.Add(btn1)
        sizer.Add(btn2)
        sizer.Add(btn3)
        panel.SetSizer(sizer)
        self.Show()

    def Action(self):
        #Keep this dialog frame open until an action has occurred.
        #The action will be from OnStep, OnPrev or OnCancel
        #Use Yield to allow other frames to be viewed
        #This pseudo-dialog returns self of relevance chain_no and result
        while self.chain_no == self.saved_chain_no:
            time.sleep(0.2)
            wx.GetApp().Yield()
        self.Close()
        return self

    def OnStep(self, evt):
        self.chain_no += 1
        #Return some relevant value or other
        self.result = int(time.time())

    def OnPrev(self, evt):
        self.chain_no -= 1
        #Return some relevant value or other (I'm just using time)
        self.result = int(time.time())

    def OnCancel(self, evt):
        self.chain_no = -1

app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()

答案 1 :(得分:0)

萨克森州罗尔夫的答案很有趣,因此我将其作为解决方案。对于这个问题,我还有一个更简单的答案,其中我拆分了函数OnOpenDialog(),以便可以在ParentFrame中等待。这个想法实际上也来自萨克森州卷的第一个答案。我添加了以下选项:当其MyDialogs打开时,ParentFrame被阻止,而其他对话框不受影响(MyFrame或其他Frame)。这个答案不是很干净,但是很简单,我不必在代码中进行太多更改。

import wx

class NonrelatedFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1, "Non-related Frame", size=(300, 300))
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        btn = wx.Button(panel, -1, "open frame")
        self.Bind(wx.EVT_BUTTON, self.OnOpenFrame, id=btn.GetId())
        sizer.Add(btn)
        text = "This is a line.\n" * 100
        txtCtrl = wx.TextCtrl(panel, -1, text, style=wx.TE_MULTILINE,
                              size=(200,200))
        sizer.Add(txtCtrl)
        panel.SetSizer(sizer)
        self.Centre()
        self.Show()

    def OnOpenFrame(self, evt):
        ParentFrame()

class ParentFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, title="Parent Frame",size=(500, 500))
        self.passActivate = None
        panel = wx.Panel(self)
        btn = wx.Button(panel, -1, "open dialog")
        self.Bind(wx.EVT_BUTTON, self.OnOpenDialog, id=btn.GetId())
        self.Centre()
        self.Show()
        self.Bind(wx.EVT_ACTIVATE, self.OnActivate)

    def OnActivate(self, evt):
        # use self.passActivate and OnActivate() to help make user not
        # be able to access anything on this form
        if self.passActivate is not None:
            self.passActivate.Raise()

    def OnOpenDialog(self, event):
        dlg1 = MyDialog(self, func2call=self.OnOpenDialog2)
        dlg1.Show()

    def OnOpenDialog2(self, val):
        dlg2 = MyDialog(self, prevAnsw=val, lastDialog=True)
        dlg2.Show()

class MyDialog(wx.Dialog): 
    def __init__(self, parent, prevAnsw=None, lastDialog=False, func2call=None): 
        wx.Dialog.__init__(self, parent, -1, title = "Dialog", style=wx.CAPTION)
        self.parent = parent
        self.parent.passActivate = self
        self.lastDialog = lastDialog
        self.func2call = func2call
        panel = wx.Panel(self)
        sizer = wx.BoxSizer(wx.VERTICAL)
        if prevAnsw is not None:
            sizer.Add(wx.StaticText(
                panel, -1, "Answer from previous Dialog: %s" % prevAnsw))
        sizer.Add(wx.StaticText(panel, -1, "Try to scroll in Non-related Frame to "
                                "see if it is blocked"))
        btn1 = wx.Button(panel, -1, "Pass 1")
        self.Bind(wx.EVT_BUTTON, self.OnPass1, id=btn1.GetId())
        sizer.Add(btn1)

        btn2 = wx.Button(panel, -1, "Pass 2")
        self.Bind(wx.EVT_BUTTON, self.OnPass2, id=btn2.GetId())
        sizer.Add(btn2)
        panel.SetSizer(sizer)

    def OnPass1(self, evt):
        if not self.lastDialog:
            self.func2call(1)
        else:
            self.parent.passActivate = None
        self.Close()


    def OnPass2(self, evt):
        if not self.lastDialog:
            self.func2call(2)
        else:
            self.parent.passActivate = None
        self.Close()


app = wx.App(0)
frame = NonrelatedFrame(None)
frame.Show()
app.MainLoop()