我第一次创建一个小的wxPython实用程序,但我遇到了一个问题。在
我想将组件添加到已经创建的框架中。为此,我销毁了框架的旧面板,并创建了一个包含所有新组件的新面板。在
1:有没有更好的方法可以动态地向面板添加内容?在
2:为什么在下面的例子中,我会遇到一个奇怪的重画错误,在这个错误中,面板只在左上角绘制,而当调整大小时,面板被正确地绘制了? (WinXP、Python 2.5、最新的wxPython)
谢谢你的帮助!在
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, 'TimeTablr')
#Variables
self.iCalFiles = ['Empty', 'Empty', 'Empty']
self.panel = wx.Panel(self, -1)
self.layoutElements()
def layoutElements(self):
self.panel.Destroy()
self.panel = wx.Panel(self, -1)
#Buttons
self.getFilesButton = wx.Button(self.panel, 1, 'Get Files')
self.calculateButton = wx.Button(self.panel, 2, 'Calculate')
self.quitButton = wx.Button(self.panel, 3, 'Quit Application')
#Binds
self.Bind(wx.EVT_BUTTON, self.Quit, id=3)
self.Bind(wx.EVT_BUTTON, self.getFiles, id=1)
#Layout Managers
vbox = wx.BoxSizer(wx.VERTICAL)
#Panel Contents
self.ctrlsToDescribe = []
self.fileNames = []
for iCalFile in self.iCalFiles:
self.ctrlsToDescribe.append(wx.TextCtrl(self.panel, -1))
self.fileNames.append(wx.StaticText(self.panel, -1, iCalFile))
#Add Components to Layout Managers
for i in range(0, len(self.ctrlsToDescribe)):
hboxtemp = wx.BoxSizer(wx.HORIZONTAL)
hboxtemp.AddStretchSpacer()
hboxtemp.Add(self.fileNames[i], 1, wx.EXPAND)
hboxtemp.AddStretchSpacer()
hboxtemp.Add(self.ctrlsToDescribe[i], 2, wx.EXPAND)
hboxtemp.AddStretchSpacer()
vbox.Add(hboxtemp)
finalHBox = wx.BoxSizer(wx.HORIZONTAL)
finalHBox.Add(self.getFilesButton)
finalHBox.Add(self.calculateButton)
finalHBox.Add(self.quitButton)
vbox.Add(finalHBox)
self.panel.SetSizer(vbox)
self.Show()
def Quit(self, event):
self.Destroy()
def getFiles(self, event):
self.iCalFiles = ['Example1','Example1','Example1','Example1','Example1','Example1']
self.layoutElements()
self.Update()
app = wx.App()
MainFrame()
app.MainLoop()
del app
我也遇到了类似的问题,面板会被挤压到右上角。{cd1>通过调用解决了。在
在您的示例中,应该在
self.panel.SetSizer(vbox)
之后调用self.panel.Fit()
1)我认为Sizer将允许您将元素插入到现有的顺序中。那可能会快一点。在
2)我看不到你在OSX上描述的行为,但你可以猜测一下,试着打电话自身布局()之前自我展示()在layoutElements中?在
相关问题 更多 >
编程相关推荐