wxpython,基本 UI 错误,按钮和列表框冲突?

发布于 2024-12-13 07:39:18 字数 1819 浏览 0 评论 0原文

我在使用 wxpython 时遇到问题。当我运行以下代码时,它将以小形式显示列表框(它只会显示大约 10x10 的列表框)。我不知道为什么会发生这种情况,并且网上没有太多资源可以帮助我解决此问题。如果我不添加任何按钮,列表框将正确显示。如果这是一个简单的修复,请原谅我,但我真的很难过。如果您不明白我的问题是什么,请运行以下代码,您就会看到。

import wx

class MyFrame(wx.Frame):
    """make a frame, inherits wx.Frame"""
    def __init__(self,parent,id):

        # create a frame, no parent, default to wxID_ANY
        wx.Frame.__init__(self, parent, id, 'Testing',
            pos=(300, 150), size=(600, 500))
        panel = wx.Panel(self)
        sampleList = ['dsakdsko0', '1', '2', '3', '4']
        listBox = wx.ListBox(panel, -1, (4,3), (100, 60), sampleList, wx.LB_SINGLE)
        listBox.SetSelection(3)

        self.SetBackgroundColour("white")

        wx.Button(self, 1, 'Close', (50, 130))
        wx.Button(self, 2, 'Random Move', (150, 130), (110, -1))

        self.Bind(wx.EVT_BUTTON, self.OnClose, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnRandomMove, id=2)

        # show the frame
        self.Show(True)
        #menu bar
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        first=wx.Menu()
        second=wx.Menu()
        first.Append(wx.NewId(),"New","Creates A new file")
        first.Append(wx.NewId(),"ADID","Yo")
        menubar.Append(first,"File")
        menubar.Append(second,"Edit")
        self.SetMenuBar(menubar)

    def OnClose(self, event):
        self.Close(True)

    def OnRandomMove(self, event):
        screensize = wx.GetDisplaySize()
        randx = random.randrange(0, screensize.x - APP_SIZE_X)
        randy = random.randrange(0, screensize.y - APP_SIZE_Y)
        self.Move((randx, randy))

if __name__=='__main__':
    application = wx.PySimpleApp()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    # start the event loop
    application.MainLoop()

非常非常感谢。

I am having problems with wxpython. When I run the following code, it will show the listbox in a small form (it will only show the listbox in about 10x10). I do not know why this is happening and there is not much resources online to help me with this. The listbox will show correctly if i do not add any buttons. Pardon me if it is an easy fix however i am truly stumped. If you do not understand what my problem is, please run the following code and you will see.

import wx

class MyFrame(wx.Frame):
    """make a frame, inherits wx.Frame"""
    def __init__(self,parent,id):

        # create a frame, no parent, default to wxID_ANY
        wx.Frame.__init__(self, parent, id, 'Testing',
            pos=(300, 150), size=(600, 500))
        panel = wx.Panel(self)
        sampleList = ['dsakdsko0', '1', '2', '3', '4']
        listBox = wx.ListBox(panel, -1, (4,3), (100, 60), sampleList, wx.LB_SINGLE)
        listBox.SetSelection(3)

        self.SetBackgroundColour("white")

        wx.Button(self, 1, 'Close', (50, 130))
        wx.Button(self, 2, 'Random Move', (150, 130), (110, -1))

        self.Bind(wx.EVT_BUTTON, self.OnClose, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnRandomMove, id=2)

        # show the frame
        self.Show(True)
        #menu bar
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        first=wx.Menu()
        second=wx.Menu()
        first.Append(wx.NewId(),"New","Creates A new file")
        first.Append(wx.NewId(),"ADID","Yo")
        menubar.Append(first,"File")
        menubar.Append(second,"Edit")
        self.SetMenuBar(menubar)

    def OnClose(self, event):
        self.Close(True)

    def OnRandomMove(self, event):
        screensize = wx.GetDisplaySize()
        randx = random.randrange(0, screensize.x - APP_SIZE_X)
        randy = random.randrange(0, screensize.y - APP_SIZE_Y)
        self.Move((randx, randy))

if __name__=='__main__':
    application = wx.PySimpleApp()
    frame=MyFrame(parent=None,id=-1)
    frame.Show()
    # start the event loop
    application.MainLoop()

Thank you very very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛装女皇 2024-12-20 07:39:18

问题涉及到父母。按钮的父级是框架,而列表框的父级是面板。将面板也赋予按钮作为其父级,问题就会消失。顺便说一句,分配自己的 ID 号码通常不是一个好主意,尤其是这么低的 ID 号码,因为这些号码可能会被使用。但如果您确实想要这样做,请按照 Robin Dunn 的 wxPython 书中的说明进行操作:

“您可以通过调用全局函数 wx.RegisterId() 来确保 wxPython 不会在应用程序中的其他位置使用您的显式 ID。以防止您的程序重复wxPython
ID,您应该避免在全局常量 wx.ID_LOWEST 和 wx.ID_HIGHEST 之间使用 ID 号。”

编辑:这是一个示例:

将其更改

wx.Button(self, 1, 'Close', (50, 130))
wx.Button(self, 2, 'Random Move', (150, 130), (110, -1))

为:

wx.Button(panel, 1, 'Close', (50, 130))
wx.Button(panel, 2, 'Random Move', (150, 130), (110, -1))

然后注册您的 ids:就

wx.RegisterId(1)
wx.RegisterId(2)

我个人而言,我只会创建我的按钮,例如这个:

closeBtn = wx.Button(panel, wx.ID_ANY, 'Close', (50, 130))
self.Bind(wx.EVT_BUTTON, self.OnClose, closeBtn)
randomBtn = wx.Button(panel, wx.ID_ANY, 'Random Move', (150, 130), (110, -1))
self.Bind(wx.EVT_BUTTON, self.OnRandomMove, randomBtn)

The problem involves the parents. Your buttons' parent is the frame while the listbox's parent is the panel. Give the panel to the buttons as their parent too and the issue will go away. By the way, it's usually not a good idea to assign your own ID numbers, especially such low ones since those might be used. But if you really want to, then follow these instructions from Robin Dunn's wxPython book:

"You can ensure that wxPython does not use your explicit ID elsewhere in the application by calling the global function wx.RegisterId(). To prevent your program from duplicating wxPython
IDs, you should avoid using ID numbers between the global constants wx.ID_LOWEST and wx.ID_HIGHEST."

EDIT: Here's an example of sorts:

Change this:

wx.Button(self, 1, 'Close', (50, 130))
wx.Button(self, 2, 'Random Move', (150, 130), (110, -1))

to this:

wx.Button(panel, 1, 'Close', (50, 130))
wx.Button(panel, 2, 'Random Move', (150, 130), (110, -1))

Then to Register your ids:

wx.RegisterId(1)
wx.RegisterId(2)

Personally, I would just create my buttons like this instead:

closeBtn = wx.Button(panel, wx.ID_ANY, 'Close', (50, 130))
self.Bind(wx.EVT_BUTTON, self.OnClose, closeBtn)
randomBtn = wx.Button(panel, wx.ID_ANY, 'Random Move', (150, 130), (110, -1))
self.Bind(wx.EVT_BUTTON, self.OnRandomMove, randomBtn)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文