问题描述
在我的应用程序中,我想在 scrolledPanel 内处理 sizer 的分配/解除分配。在我的第一次尝试中,我隐藏/显示了 sizer 的内容,但这导致了很多问题。隐藏的 sizers 会“呆在那里”,有效地占用空间,不会及时更新,等等。更好的解决方案是一遍又一遍地销毁和创建它们。
但这也是它的问题。 scrolledPanel 在我更新时闪烁,并且似乎资源很重。在我的实际应用中,我将按钮和复选框的引用放在列表中,处理它们变得更加困难。
所以,如果有人有更好的解决方案,我会全神贯注!
我正在寻找隐藏/显示解决方案!重置 scrolledPanel 的大小以仅容纳它当前拥有的 sizer 的东西也很好。
谢谢!
import wx
import wx.lib.scrolledpanel as scrolled
class WaterDataBase(wx.Frame):
def __init__(self,parent):
wx.Frame.__init__(self,parent)
self.setupSizers()
self.setupData()
def setupSizers(self):
masterSizer = wx.BoxSizer(wx.HORIZONTAL)
itemsSizer = wx.BoxSizer(wx.VERTICAL)
itemsSizer.Add(self.addSearchControl(),flag=wx.ALL,border=7)
self.scrolledPanelSizer = wx.BoxSizer(wx.VERTICAL)
self.scrolled_panel = scrolled.ScrolledPanel(self,wx.ID_ANY,size=(200,200))
self.scrolled_panel.SetSizer(self.scrolledPanelSizer)
itemsSizer.Add(self.scrolled_panel,border=7)
masterSizer.Add(itemsSizer)
self.scrolled_panel.SetupScrolling()
self.SetSizer(masterSizer)
def addSearchControl(self):
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.searchField = wx.TextCtrl(self,-1)
self.searchField.Bind(wx.EVT_TEXT,self.OnSearched)
sizer.Add(self.searchField)
return sizer
def setupData(self):
self.words = "I'm trying to make this work,please. Let's keep it on! The day is beautiful today. Together we are stronger!".split()
for word in self.words:
self.addSizerToPanel(word)
def createSizer(self,word):
# Creates a sizer with a CheckBox and a StaticText to display.
sizer = wx.BoxSizer(wx.HORIZONTAL)
checkBox = wx.CheckBox(self.scrolled_panel,-1)
text = wx.StaticText(self.scrolled_panel,-1,word)
sizer.Add(checkBox,border=5)
sizer.Add(text,flag=wx.LEFT,border=5)
return sizer
def addSizerToPanel(self,word):
sizer = self.createSizer(word)
self.scrolledPanelSizer.Add(sizer,border=5)
def OnSearched(self,event):
query = self.searchField.GetValue().lower()
result = []
# If query's empty,print all words
if not query or query.isspace():
for word in self.words:
result.append(word)
else:
for word in self.words:
if word.lower().find(query) != -1:
result.append(word)
# Destroy all panel sizers and put exactly the ones we want.
self.scrolled_panel.DestroyChildren()
for word in result:
self.addSizerToPanel(word)
self.scrolled_panel.Layout()
self.scrolled_panel.Scroll(0,0) # Using this to cause the scrollPanel get back to the top.
app = wx.App()
frame = WaterDataBase(None).Show()
app.MainLoop()
所以,看来我终于做到了。我仍然需要有关重置 scrolledPanel 大小的建议。 xD
import wx
import wx.lib.scrolledpanel as scrolled
class Frame(wx.Frame):
def __init__(self,parent)
self.sizerRefs = []
self.words = []
self.setupSizers()
self.setupData()
def setupSizers(self):
masterSizer = wx.BoxSizer(wx.HORIZONTAL)
itemsSizer = wx.BoxSizer(wx.VERTICAL)
itemsSizer.Add(self.addSearchControl(),please. Let's keep it on! The day is beautiful today. Together we are stronger!".split()
for i in range(0,len(self.words)):
self.addSizerToPanel(i)
def createSizer(self,index):
sizer = wx.BoxSizer(wx.HORIZONTAL)
checkBox = wx.CheckBox(self.scrolled_panel,self.words[index])
sizer.Add(checkBox,border=5)
self.sizerRefs.append(sizer)
return sizer
def addSizerToPanel(self,index):
sizer = self.createSizer(index)
self.scrolledPanelSizer.Add(sizer,border=5)
def hideAllSizers(self):
for sizer in self.sizerRefs:
sizer.ShowItems(False)
def unhideSizer(self,index):
self.sizerRefs[index].ShowItems(True)
def OnSearched(self,event):
query = self.searchField.GetValue().lower()
result = [] # Storing the indexes of the words found
# If query's empty,print all words
if not query or query.isspace():
for i in range(0,len(self.words)):
result.append(i)
else:
for i in range(0,len(self.words)):
if self.words[i].lower().find(query) != -1:
result.append(i)
# Hides all panel sizers and unhide exactly the ones we want.
self.hideAllSizers()
for i in range(0,len(result)):
self.unhideSizer(result[i])
self.scrolled_panel.Layout()
self.scrolled_panel.Scroll(0,0) # Using this to cause the scrollPanel get back to the top.
app = wx.App()
frame = Frame(None).Show()
app.MainLoop()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)