gpt4 book ai didi

python - 派生自 wx.Dialog 的通用 MessageBox

转载 作者:太空宇宙 更新时间:2023-11-03 18:35:07 25 4
gpt4 key购买 nike

正如其他一些主题中所指出的,wx.MessageDialog 不响应许多 API 函数,例如来自 Destroy() 的外部调用等。因此需要构建一个从wx.Dialog派生的wx.GenericMessageBox。这是:

enter image description here

import  wx

class GenericMessageBox(wx.Dialog):
def __init__(self, parent, text, title = ''):
wx.Dialog.__init__(self, parent, -1, title = title, size = (360,120), style = wx.DEFAULT_DIALOG_STYLE)
panel = wx.Panel(self, wx.ID_ANY, size = (360, 50), pos = (0,0))
panel.SetBackgroundColour('#FFFFFF')
label = wx.StaticText(panel, -1, text, pos = (50,20))
panel2 = wx.Panel(self, wx.ID_ANY, size = (360, 40), pos = (0, 50))
btn = wx.Button(panel2, wx.ID_OK, pos = (250,7))
self.ShowModal()


app = wx.App()
frame = wx.Frame(None, 0, 'Test')
frame.Show()
GenericMessageBox(frame, 'This is a message box that is derived from wx.Dialog. You can Destroy() it from anywhere in the code.', 'Test')
app.MainLoop()

wx.lib.agw.genericmessagedialog不同,这个目标是尽可能接近 native 操作系统的外观(这里是Windows外观)。 [genericmessagedialog 按钮中有图片,这与 Windows 的原生外观不同]

如果 StaticText 需要两行,如何改进此对话框,使其大小自动增加?

此外,“确定”按钮 (x,y) 的位置很好,并且位于我机器上的灰色面板的中心,但在其他平台上是否会相同?

(我认为这样的代码片段可能对社区有用。)

最佳答案

要管理布局,最好使用 Sizer 而不是位置。

您可以尝试使用以下代码:

使用 self.setMessageLine(Num) 设置输出以查看差异。

    import wx

class MyDialog ( wx.Dialog ):

def __init__( self, parent ):
wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 300,200 ), style = wx.DEFAULT_DIALOG_STYLE )

#set the minimum Size of the frame to Fit()
self.SetSizeHintsSz( wx.Size( 300,-1 ), wx.DefaultSize )

fgSizer = wx.FlexGridSizer( 2, 1, 0, 0 )
fgSizer.AddGrowableCol( 0 )
fgSizer.AddGrowableRow( 0 )
fgSizer.SetFlexibleDirection( wx.BOTH )
fgSizer.SetNonFlexibleGrowMode( wx.FLEX_GROWMODE_ALL )

self.m_panel = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.SIMPLE_BORDER|wx.TAB_TRAVERSAL )
gSizer = wx.GridSizer( 1, 1, 0, 0 )

self.m_staticText = wx.StaticText( self.m_panel, wx.ID_ANY, u"test", wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_staticText.Wrap( 10 )
gSizer.Add( self.m_staticText, 0, wx.ALIGN_CENTER|wx.ALL, 5 )


self.m_panel.SetSizer( gSizer )
self.m_panel.Layout()
gSizer.Fit( self.m_panel )
fgSizer.Add( self.m_panel, 1, wx.EXPAND |wx.ALL, 5 )

m_sdbSizer = wx.StdDialogButtonSizer()
self.m_sdbSizerOK = wx.Button( self, wx.ID_OK )
m_sdbSizer.AddButton( self.m_sdbSizerOK )
self.m_sdbSizerCancel = wx.Button( self, wx.ID_CANCEL )
m_sdbSizer.AddButton( self.m_sdbSizerCancel )
m_sdbSizer.Realize()

fgSizer.Add( m_sdbSizer, 1, wx.ALL|wx.EXPAND, 5 )
self.SetSizer( fgSizer )
self.Layout()
self.Centre( wx.BOTH )
self.setMessageLine(10)

def setMessageLine(self, messageLine):
msg = ""
for i in range(0, messageLine):
msg += "Line %d \n" % i

self.m_staticText.SetLabel(msg)
self.Fit()

if __name__ == '__main__':
app = wx.App()
dlg = MyDialog(None)
dlg.ShowModal()
app.MainLoop()
pass

enter image description here enter image description here

关于python - 派生自 wx.Dialog 的通用 MessageBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21731609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com