Wxpython面板的裁剪仅在左上角显示了一个小框 [英] Wxpython panel is cropped with only a small box shown at the top left hand corner
问题描述
我正在使用wx的Hide()和Show()通过隐藏面板并在同一帧中显示下一个面板来执行下一页"效果(虽然不太确定我是否正确执行了操作).在某些页面上,该面板只是左上角的小裁切版本,而其他一些面板则可以正常工作(显示完整内容).我该如何解决这个问题?
I am using Hide() and Show() from wx to do the "next page" effect by hiding a panel and showing the next one but in the same frame (not very sure if I am doing it correctly though). At certain pages, the panel is just a small cropped version at the top left corner while some other panels can work normally (display the full thing). How do I solve this problem?
我在stackoverflow上看到了有关面板或框架的子/父项的某些信息,并尝试更改我的代码,但是它不起作用,不太确定如何正确执行操作.
I saw something on stackoverflow about child/parent of the panel or frame and tried changing my code but it does not work, not very sure about how to do it correctly.
class MyPanel(wx.Panel):
def __init__(self, parent):
#Constructor
wx.Panel.__init__(self, parent=parent)
#self.SetBackgroundStyle(wx.BG_STYLE_CUSTOM)
#This is for older versions of wx
self.frame = parent
self.Bind(wx.EVT_ERASE_BACKGROUND, self.OnEraseBackground)
self.Layout()
def OnEraseBackground(self, evt):
#Add background pic
#From ColourDB.py
dc = evt.GetDC()
if not dc:
dc = wx.ClientDC(self)
rect = self.GetUpdateRegion().GetBox()
dc.SetClippingRect(rect)
dc.Clear()
bmp = wx.Bitmap("RszHive.jpg")
dc.DrawBitmap(bmp, 0, 0)
class StartPage(wx.Frame):
def __init__(self, current_dt):
#Settings for frame
super().__init__(parent=None, title='Test', size=(850,790))
#setting up main panel (home page)
self.panel = MyPanel(self)
self.current_dt = current_dt
#so that frame will be in the center of the screen
self.Center()
self.vert_sizer = wx.BoxSizer(wx.VERTICAL)
from Database import DataBase, OperatingHours, GetDayTime, GetMenuByDayTime
dDataBase = DataBase("Full_Menu_Database.txt")
dOperatingHours = OperatingHours("Operating Hours.txt")
# Convert to a tuple needed for the functions
tDayTime = GetDayTime(self.get_dt())
# Function to get menu dictionary by date and time
# Will return an empty dictionary if no food/stores are available
self.stores_open = GetMenuByDayTime(dDataBase, dOperatingHours, tDayTime)
if self.stores_open == {}:
self.ophours = wx.StaticText(self.panel, -1, style=wx.ALIGN_CENTER)
self.ophours.SetLabel("Test")
self.ophours_font = wx.Font(19, wx.TELETYPE, wx.NORMAL, wx.NORMAL)
self.ophours.SetFont(self.ophours_font)
self.vert_sizer.Add(self.ophours, 0, wx.ALL | wx.CENTER, 10)
else:
self.store_names, self.stores_ = [], []
for onestorename in self.stores_open.keys():
self.store_names.append(onestorename)
self.stores_.append(self.stores_open[onestorename])
#btn for store1
store_btn1 = wx.Button(self.panel, label= self.store_names[0])
store_btn1.Bind(wx.EVT_BUTTON, self.click_store1)
self.vert_sizer.Add(store_btn1, 0, wx.ALL | wx.CENTER, 5)
#btn for store2 if have
if len(self.store_names) > 1:
store_btn2 = wx.Button(self.panel, label=self.store_names[1])
store_btn2.Bind(wx.EVT_BUTTON, self.click_store2)
self.vert_sizer.Add(store_btn2, 0, wx.ALL | wx.CENTER, 5)
# btn for store3 if have
if len(self.store_names) > 2:
store_btn3 = wx.Button(self.panel, label=self.store_names[2])
store_btn3.Bind(wx.EVT_BUTTON, self.click_store3)
self.vert_sizer.Add(store_btn3, 0, wx.ALL | wx.CENTER, 5)
# btn for store4 if have
if len(self.store_names) > 3:
store_btn4 = wx.Button(self.panel, label=self.store_names[3])
store_btn4.Bind(wx.EVT_BUTTON, self.click_store4)
self.vert_sizer.Add(store_btn4, 0, wx.ALL | wx.CENTER, 5)
# btn for store5 if have
if len(self.store_names) > 4:
store_btn5 = wx.Button(self.panel, label=self.store_names[4])
store_btn5.Bind(wx.EVT_BUTTON, self.click_store5)
self.vert_sizer.Add(store_btn5, 0, wx.ALL | wx.CENTER, 5)
self.SetSizer(self.vert_sizer)
self.Layout()
self.Show()
推荐答案
我也遇到了很长时间的问题,不知道解决方案.尺寸调整器不起作用(正如我预期的那样)
I also had the problem a very long time and did not know the solution. The sizers did not work (as I expected)
对我来说,问题是面板没有(或尺寸不正确).解决的方法很简单:
For me, the problem was, that the panel had no (or the incorrect size). The solution was eiter:
panel.Fit()
或者 panel.SetSize(x,y)
另一种可能性是,首先将面板添加到尺寸调整器中.然后将它们设置到框架.然后,将按钮放入sizer中-并将它们添加到面板中.这也解决了面板尺寸不正确的问题.
Another possibility was, to first add the panel into a sizer. And then set them to the frame. Afterwards put the buttons into the sizer - and add them to the panel. This also solves the incorrect size of the panel.
这篇关于Wxpython面板的裁剪仅在左上角显示了一个小框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!