当我在框架中使用一个按钮并使用坐标定位它时,小部件显示不正确。它占用了整个框架。当我在框架中添加另一个按钮时,两个按钮均正确显示。为什么会这样?
我已经在运行于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()
我希望一个Button示例可以在指定位置显示,就像两个按钮示例一样。这是wxPython的错误吗?
答案 0 :(得分: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()
它会表现出来。