在wxPython中显示图像网格
我试图在wxPython中显示一个网格图像。我正在使用GridSizer创建一个网格,向其中添加静态位图。但由于某些原因,我在网格的第一个位置只看到一个图像。我不确定我会错在哪里。这是我的代码和相应的输出在wxPython中显示图像网格,python,image,wxpython,Python,Image,Wxpython,我试图在wxPython中显示一个网格图像。我正在使用GridSizer创建一个网格,向其中添加静态位图。但由于某些原因,我在网格的第一个位置只看到一个图像。我不确定我会错在哪里。这是我的代码和相应的输出 import wx import sys import glob MAIN_PANEL = wx.NewId() class CommunicationApp(wx.App): """This is the class for the communi
import wx
import sys
import glob
MAIN_PANEL = wx.NewId()
class CommunicationApp(wx.App):
"""This is the class for the communication tool application.
"""
def __init__(self, config=None, redirect=False):
"""Instantiates an application.
"""
wx.App.__init__(self, redirect=redirect)
self.cfg = config
self.mainframe = CommunicationFrame(config=config,
redirect=redirect)
self.mainframe.Show()
def OnInit(self):
# self.SetTopWindow(self.mainframe)
return True
class CommunicationFrame(wx.Frame):
"""Frame of the Communication Application.
"""
def __init__(self, config, redirect=False):
"""Initialize the frame.
"""
wx.Frame.__init__(self, parent=None,
title="CMC Communication Tool",
style=wx.DEFAULT_FRAME_STYLE)
self.imgs = glob.glob('../img/img*.png')
self.panel = CommuniationPanel(parent=self,
pid=MAIN_PANEL,
config=config)
# # Gridbagsizer.
nrows, ncols = 3, 4
self.grid = wx.GridSizer(rows=nrows, cols=ncols)
# Add images to the grid.
for r in xrange(nrows):
for c in xrange(ncols):
_n = ncols * r + c
_tmp = wx.Image(self.imgs[_n],
wx.BITMAP_TYPE_ANY)
_temp = wx.StaticBitmap(self.panel, wx.ID_ANY,
wx.BitmapFromImage(_tmp))
self.grid.Add(_temp, 0, wx.EXPAND)
self.grid.Fit(self)
# set to full screen.
# self.ShowFullScreen(not self.IsFullScreen(), 0)
class CommuniationPanel(wx.Panel):
"""Panel of the Communication application frame.
"""
def __init__(self, parent, pid, config):
"""Initialize the panel.
"""
wx.Panel.__init__(self, parent=parent, id=pid)
# CALLBACK BINDINGS
# Bind keyboard events.
self.Bind(wx.EVT_KEY_UP, self.on_key_up)
def on_key_up(self, evt):
"""Handles Key UP events.
"""
code = evt.GetKeyCode()
print code, wx.WXK_ESCAPE
if code == wx.WXK_ESCAPE:
sys.exit(0)
def main():
app = CommunicationApp()
app.MainLoop()
if __name__ == '__main__':
main()
这是输出。
我想看到(或期望看到)的是不同图像的3X4网格
我的代码有什么问题?我该如何修复它呢?您似乎忘了为面板设置尺寸 试着把它放到框架的
\uuuuu init\uuuu
self.panel.SetSizer(self.grid)
我还没有试着运行代码,但也许你忘了调用
SetSizer
self.panel.SetSizer(self.grid)
@GP89我知道这是一件简单但关键的事情,但我没有做到。成功了!谢谢你的帮助,没问题。我会把它作为一个答案贴出来,以防它对其他人有帮助future@siva82kb,使用sys.exit不是关闭应用程序的“好”方法,为什么不使用parent.close()?我还建议不要将globals用作主面板ID,我认为在当前的wxPython代码中,您几乎不需要保留/存储ID。@Werner谢谢您的建议。我已经修改了我的代码。