首页 > 解决方案 > 是否有可能创建一个位于所有其他窗口之上的消息对话框?

问题描述

我正在尝试为我的班级管理软件创建一个 GUI。我需要一条消息,该消息会弹出给老师并询问他是否要停止操作。由于该消息必须出现在所有其他窗口的顶部

我尝试使用以下标志创建 wx.message_dialog:style = wx.STAY_ON_TOP 但它不起作用

def stopscreen(self): 
    stopBox = wx.MessageDialog(None, "do you want to stop","stop controling", style=wx.STAY_ON_TOP | wx.YES_NO | wx.CENTRE)
    stopBoxAns = stopBox.ShowModal()
    if stopBoxAns == 5103:
        stopBox.Destroy()
        return 1### ok
    if stopBoxAns == 5104:
        stopBox.Destroy()
        return 2### cancel

标签: pythonpython-2.7wxpython

解决方案


It should work, as you have coded it, unless you are on a Mac.

wx.STAY_ON_TOP: Makes the message box stay on top of all other windows and not only just its parent (currently implemented only under MSW and GTK)

If you are still having trouble, try setting the window style to STAY_ON_TOP as well.

#!/usr/bin/env python

import wx

#---------------------------------------------------------------------------

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, -1)
        panel = wx.Panel(self)
        self.SetWindowStyle(wx.STAY_ON_TOP)
        button = wx.Button(panel, -1, "Show MessageDialog", (50,50))
        self.Bind(wx.EVT_BUTTON, self.OnButton)
        self.Show()

    def OnButton(self, event):
        dlg = wx.MessageDialog(self, 'Hello from wxPython!',
                               'A Message Box',
                               wx.YES | wx.NO | wx.ICON_INFORMATION | wx.STAY_ON_TOP
                               )
        dlg.ShowModal()
        dlg.Destroy()


if __name__ == "__main__":
    app = wx.App(False)
    MyFrame(None)
    app.MainLoop()

推荐阅读