【问题标题】:wxPython: widget positioned incorrectly when adding one widget to a framewxPython:将一个小部件添加到框架时,小部件位置不正确
【发布时间】:2019-04-21 17:25:35
【问题描述】:

当我在 Frame 中使用一个 Button 并使用坐标定位它时,小部件无法正确显示。它占据了整个 Frame。当我将另一个按钮添加到框架时,两个按钮都正确显示。这是为什么呢?

我已经在 Mac OS 10.4、Mac OS 10.12 和 Windows 10 上运行的 wxPython 上进行了尝试。每次都错误地显示一个小部件。

# This will display the buttons incorrectly
import wx

app = wx.App()
frame = wx.Frame(None, wx.ID_ANY, "Main Window")
button1 = wx.Button(frame, wx.ID_ANY, "Button 1", (50, 50))
frame.Show()
app.MainLoop()
# This will display the buttons correctly
import wx

app = wx.App()
frame = wx.Frame(None, wx.ID_ANY, "Main Window")
button1 = wx.Button(frame, wx.ID_ANY, "Button 1", (50, 50))
button2 = wx.Button(frame, wx.ID_ANY, "Button 2", (160, 50))
frame.Show()
app.MainLoop()

我希望一个按钮示例像两个按钮示例一样显示在指定位置。这是 wxPython 的错误吗?

【问题讨论】:

    标签: widget position wxpython


    【解决方案1】:

    将框架视为小部件的包装。通常一个框架将包含一个或多个Panels
    从文件: if the frame has exactly one child window, not counting the status and toolbar, this child is resized to take the entire frame client area.

    A panel is a window on which controls are placed. It is usually placed within a frame. Its main feature over its parent class wx.Window is code for handling child windows and TAB traversal.

    panel 合并到您的代码中:

    import wx
    app = wx.App()
    frame = wx.Frame(None, wx.ID_ANY, "Window")
    panel = wx.Panel(frame,wx.ID_ANY)
    button1 = wx.Button(panel, wx.ID_ANY, "Button 1", pos=(50, 50))
    frame.Show()
    app.MainLoop()
    

    它会自动运行。

    【讨论】:

    猜你喜欢
    • 2011-05-04
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    • 2021-06-08
    • 2021-03-01
    • 2020-09-22
    • 2011-09-27
    • 2012-08-05
    相关资源
    最近更新 更多