📌  相关文章
📜  wxPython – wx.Button 中的 SetDefault()函数

📅  最后修改于: 2022-05-13 01:55:37.406000             🧑  作者: Mango

wxPython – wx.Button 中的 SetDefault()函数

在本文中,我们将学习与 wxPython 的 wx.Button 类关联的 SetDefault()函数。这会将按钮设置为其顶层窗口(例如面板或包含它的对话框)中的默认项。
正常情况下,按下返回键会在按下返回键时按下默认按钮。

代码示例:

import wx
  
  
class MyDialog(wx.Dialog):
    def __init__(self, parent, title):
        super(MyDialog, self).__init__(parent, title = title, size =(250, 175))
        panel = wx.Panel(self)
        self.btn = wx.Button(panel, wx.ID_OK, label ="Default",
                                  size =(50, 20), pos =(75, 50))
        self.btn1 = wx.Button(panel, wx.ID_OK, label ="Not Default",
                                     size =(90, 20), pos =(75, 100))
  
  
class Mywin(wx.Frame):
  
    def __init__(self, parent, title):
        super(Mywin, self).__init__(parent, title = title, size =(250, 150))
        self.InitUI()
  
    def InitUI(self):
        panel = wx.Panel(self)
        btn = wx.Button(panel, label ="Click", pos =(75, 10))
        btn.Bind(wx.EVT_BUTTON, self.OnModal)
        # SET BUTTON AS DEFAULT
        btn.SetDefault()
  
        self.SetMinSize((600, 400))
        self.Centre()
        self.Show(True)
  
    def OnModal(self, event):
        a = MyDialog(self, "Dialog").ShowModal()
  
ex = wx.App()
Mywin(None, 'Window')
ex.MainLoop()

输出窗口: