wxPython 静态文本问题

0 投票
2 回答
988 浏览
提问于 2025-04-17 23:11

我在使用wxPython写代码时遇到了一个问题,StaticText这个部分让其他所有东西似乎都消失了。

注意:这是我第一次使用wxPython,而且我对编程还很陌生,所以请尽量给我一个清楚的解释。谢谢!

import wx

APP_EXIT = 1
pos1 = (150, 200)

class scoutingSet(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(scoutingSet, self).__init__(*args, **kwargs)

        self.InitUI()

    def InitUI(self):

         ############################################################
         # MENUBARS AND MENUITEMS
         menuBar = wx.MenuBar()
         fileMenu = wx.Menu()
         fileMenu2 = wx.Menu()

         openSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Open')
         openSheet.SetBitmap(wx.Bitmap('open.png'))
         fileMenu.AppendItem(openSheet)
         fileMenu.AppendSeparator()

         saveSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Save')
         saveSheet.SetBitmap(wx.Bitmap('save.png'))
         fileMenu.AppendItem(saveSheet)
         fileMenu.AppendSeparator()

         quitSheet = wx.MenuItem(fileMenu, APP_EXIT, 'Quit')
         quitSheet.SetBitmap(wx.Bitmap('close.png'))
         fileMenu.AppendItem(quitSheet)
         self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)

         fileMenu2.Append(100, '&About')
         self.Bind(wx.EVT_MENU, self.aboutBox, id=100)

         menuBar.Append(fileMenu, 'File')
         menuBar.Append(fileMenu2, 'Information')
         self.SetMenuBar(menuBar)

         ############################################################
         # BUTTONS AND CONTROL

         panel = wx.Panel(self)
         closebutton = wx.Button(panel, label = 'Close\nClose', pos = (20, 30))

         closebutton.Bind(wx.EVT_BUTTON, self.OnQuit)

         ############################################################
         # STATIC TEXTS


         ############################################################
         # TEXT CONTROL BOXES


         wx.TextCtrl(panel, pos = pos1, size = (50, 50))
         wx.TextCtrl(panel, pos = (300, 400), size = (50, 50))

         ############################################################
         # SETTINGS

         self.stuff(self)

         self.Maximize()
         self.SetTitle('Scouting Sheet')
         self.Centre()
         self.Show(True)

         ############################################################

         # Quitting

    def OnQuit(self, e):
        self.Close()

    # Info in 

    def aboutBox(self, e):
        desc = """This is the SOTAbots 2014 scouting sheet for the FRC 2014 game Aerial Assist"""

        infoInAbout = wx.AboutDialogInfo()
        infoInAbout.SetIcon(wx.Icon('scouting.png', wx.BITMAP_TYPE_PNG))
        infoInAbout.SetName('Scouting Sheet')
        infoInAbout.SetVersion('1.0')
        infoInAbout.SetDescription(desc)
        infoInAbout.AddDeveloper('Franklin Lyon\nLucas Grillos')
        wx.AboutBox(infoInAbout)

    def stuff(self, e):
        textpnl = wx.StaticText(self,-1 , label='Watermark', pos=(20, 30))
        textpnl.SetForegroundColour('white')
        textpnl.SetBackgroundColour('blue')

def main():
    ex = wx.App()
    scoutingSet(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()

注意:我把Static Text放在了一个函数里,但即使在InitUI函数里面,这个问题依然存在。问题出在StaticText的显示上,因为如果我把这部分代码注释掉,其他的内容就都正常显示了。

提前谢谢大家。

2 个回答

0

你的界面组件布局得不太好。我建议你使用sizers来进行合理的布局。BoxSizer使用起来很简单。这里有一个不错的教程,讲的是布局管理

你的代码在你提供了panel的大小后就能正常工作了。你可以用这一行代码panel = wx.Panel(self, -1, size=(800,800)),这样你就能看到所有的组件了!我还调整了你静态文本的位置,因为它和按钮重叠了。

请注意,尽量不要在代码中使用像panel这样的名字。可以用myPanel或者panelA之类的名字。

有效代码:在Windows 8和wxPython v3.0上测试过

import wx

APP_EXIT = 1
pos1 = (150, 200)

class scoutingSet(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(scoutingSet, self).__init__(*args, **kwargs)

        self.InitUI()

    def InitUI(self):

         ############################################################
         # MENUBARS AND MENUITEMS
         menuBar = wx.MenuBar()
         fileMenu = wx.Menu()
         fileMenu2 = wx.Menu()

         openSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Open')
         openSheet.SetBitmap(wx.Bitmap('open.png'))
         fileMenu.AppendItem(openSheet)
         fileMenu.AppendSeparator()

         saveSheet = wx.MenuItem(fileMenu, wx.ID_ANY, 'Save')
         saveSheet.SetBitmap(wx.Bitmap('save.png'))
         fileMenu.AppendItem(saveSheet)
         fileMenu.AppendSeparator()

         quitSheet = wx.MenuItem(fileMenu, APP_EXIT, 'Quit')
         quitSheet.SetBitmap(wx.Bitmap('close.png'))
         fileMenu.AppendItem(quitSheet)
         self.Bind(wx.EVT_MENU, self.OnQuit, id=APP_EXIT)

         fileMenu2.Append(100, '&About')
         self.Bind(wx.EVT_MENU, self.aboutBox, id=100)

         menuBar.Append(fileMenu, 'File')
         menuBar.Append(fileMenu2, 'Information')
         self.SetMenuBar(menuBar)

         ############################################################
         # BUTTONS AND CONTROL

         panel = wx.Panel(self, -1, size=(800,800))
         closebutton = wx.Button(panel, label = 'Close\nClose', pos = (20, 30))

         closebutton.Bind(wx.EVT_BUTTON, self.OnQuit)

         ############################################################
         # STATIC TEXTS


         ############################################################
         # TEXT CONTROL BOXES


         wx.TextCtrl(panel, pos = pos1, size = (50, 50))
         wx.TextCtrl(panel, pos = (300, 400), size = (50, 50))

         ############################################################
         # SETTINGS

         self.stuff(self)

         self.Maximize()
         self.SetTitle('Scouting Sheet')
         self.Centre()
         self.Show(True)

         ############################################################

         # Quitting

    def OnQuit(self, e):
        self.Close()

    # Info in 

    def aboutBox(self, e):
        desc = """This is the SOTAbots 2014 scouting sheet for the FRC 2014 game Aerial Assist"""

        infoInAbout = wx.AboutDialogInfo()
        infoInAbout.SetIcon(wx.Icon('scouting.png', wx.BITMAP_TYPE_PNG))
        infoInAbout.SetName('Scouting Sheet')
        infoInAbout.SetVersion('1.0')
        infoInAbout.SetDescription(desc)
        infoInAbout.AddDeveloper('Franklin Lyon\nLucas Grillos')
        wx.AboutBox(infoInAbout)

    def stuff(self, e):
        textpnl = wx.StaticText(self,-1 , label='Watermark', pos=(100, 100))
        textpnl.SetForegroundColour('white')
        textpnl.SetBackgroundColour('blue')

def main():
    ex = wx.App()
    scoutingSet(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()

希望这些对你有帮助。

0

我觉得 wx.StaticText 是放在 self(wx.Frame)上,而不是放在面板上,虽然 wx.TextCtrl 是放在面板里的。如果你把 wx.StaticText 放在面板上会怎么样呢?

另外,我觉得你需要一个 Sizer(比如 wx.BoxSizer)来管理布局。你可以在这个链接找到关于 wx.BoxSizer 的教程:http://zetcode.com/wxpython/layout/。在 initUI 里,我会做类似这样的事情:

 txtctrl1 = wx.TextCtrl(panel, pos = pos1, size = (50, 50))
 txtctrl2 = wx.TextCtrl(panel, pos = (300, 400), size = (50, 50))
 textpnl = wx.StaticText(panel,-1 , label='Watermark', pos=(20, 30))

 vbox = wx.BoxSizer(wx.VERTICAL)
 vbox.add(txtctrl1, 1, wx.EXPAND | wx.ALL, 5)
 vbox.add(txtctrl2, 1, wx.EXPAND | wx.ALL, 5)
 vbox.add(textpnl , 1, wx.EXPAND | wx.ALL, 5)
 vbox.add(closebutton, 0, wx.EXPAND | wx.ALL, 5)

 panel.SetSizer(vbox)

希望这对你有帮助。

撰写回答