wxPython控制间距
wxPython control spacing
我正在编写一个基本的 Intinerary Manager。只是一个简单的副项目,让我自己熟悉使用 wxPython 创建 GUI。这是我的第一次尝试,我似乎无法在其他任何地方找到任何关于此问题的参考资料。
我的代码如下:
import wx
class mainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(650, 1000))
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
topLabel = wx.StaticText(panel, size = (-1, -1), label="Itinerary")
vbox.Add(topLabel, 1, wx.EXPAND)
self.listBox = wx.ListCtrl(panel, style = wx.LC_LIST)
self.listBox.InsertColumn(0, "Test Column")
self.listBox.Append(["This is an item"])
self.listBox.Append(["This is another item"])
vbox.Add(self.listBox, 1, wx.EXPAND | wx.ALL, 20)
panel.SetSizer(vbox)
self.Show(True)
app = wx.App(False)
frame = mainWindow(None, "Itinerary Manager")
app.MainLoop()
出于某种原因,这导致顶部的 StaticText 元素与其下方的 ListCtrl 之间存在巨大的空白。我已经尝试了一些变通方法,包括将每个的父级设置为自身,但这给出了相同的输出。我该怎么做才能确保这 2 个控件彼此之间没有(或非常小的)边距?
问题是您要让 wx.StaticText
控件扩展并占据应用程序宽度的一半。你不想要那个。因此,将第 13 行更改为:
vbox.Add(topLabel, 1, wx.EXPAND)
对此:
vbox.Add(topLabel, 0, wx.ALL, 5)
数字 1 告诉 wxPython 小部件应该占据多少比例。由于静态文本和列表框都设置为一个,因此它们各自占用完全相同的数量。大多数时候,静态文本控件不应展开。无论如何,这是完整的代码:
import wx
class mainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(650, 1000))
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
topLabel = wx.StaticText(panel, size = (-1, -1), label="Itinerary")
vbox.Add(topLabel, 0, wx.ALL, 5)
self.listBox = wx.ListCtrl(panel, style = wx.LC_LIST)
self.listBox.InsertColumn(0, "Test Column")
self.listBox.Append(["This is an item"])
self.listBox.Append(["This is another item"])
vbox.Add(self.listBox, 1, wx.EXPAND | wx.ALL, 20)
panel.SetSizer(vbox)
self.Show(True)
app = wx.App(False)
frame = mainWindow(None, "Itinerary Manager")
app.MainLoop()
我正在编写一个基本的 Intinerary Manager。只是一个简单的副项目,让我自己熟悉使用 wxPython 创建 GUI。这是我的第一次尝试,我似乎无法在其他任何地方找到任何关于此问题的参考资料。
我的代码如下:
import wx
class mainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(650, 1000))
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
topLabel = wx.StaticText(panel, size = (-1, -1), label="Itinerary")
vbox.Add(topLabel, 1, wx.EXPAND)
self.listBox = wx.ListCtrl(panel, style = wx.LC_LIST)
self.listBox.InsertColumn(0, "Test Column")
self.listBox.Append(["This is an item"])
self.listBox.Append(["This is another item"])
vbox.Add(self.listBox, 1, wx.EXPAND | wx.ALL, 20)
panel.SetSizer(vbox)
self.Show(True)
app = wx.App(False)
frame = mainWindow(None, "Itinerary Manager")
app.MainLoop()
出于某种原因,这导致顶部的 StaticText 元素与其下方的 ListCtrl 之间存在巨大的空白。我已经尝试了一些变通方法,包括将每个的父级设置为自身,但这给出了相同的输出。我该怎么做才能确保这 2 个控件彼此之间没有(或非常小的)边距?
问题是您要让 wx.StaticText
控件扩展并占据应用程序宽度的一半。你不想要那个。因此,将第 13 行更改为:
vbox.Add(topLabel, 1, wx.EXPAND)
对此:
vbox.Add(topLabel, 0, wx.ALL, 5)
数字 1 告诉 wxPython 小部件应该占据多少比例。由于静态文本和列表框都设置为一个,因此它们各自占用完全相同的数量。大多数时候,静态文本控件不应展开。无论如何,这是完整的代码:
import wx
class mainWindow(wx.Frame):
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, title=title, size=(650, 1000))
panel = wx.Panel(self)
vbox = wx.BoxSizer(wx.VERTICAL)
topLabel = wx.StaticText(panel, size = (-1, -1), label="Itinerary")
vbox.Add(topLabel, 0, wx.ALL, 5)
self.listBox = wx.ListCtrl(panel, style = wx.LC_LIST)
self.listBox.InsertColumn(0, "Test Column")
self.listBox.Append(["This is an item"])
self.listBox.Append(["This is another item"])
vbox.Add(self.listBox, 1, wx.EXPAND | wx.ALL, 20)
panel.SetSizer(vbox)
self.Show(True)
app = wx.App(False)
frame = mainWindow(None, "Itinerary Manager")
app.MainLoop()