2012-09-29 40 views
3

我在Ubuntu 12.04上使用Python 2.7和wx。Wx Python圖像查看器 - atfer打開圖像文件菜單太緊

我用wx在Python中編寫了一個微小的圖像查看器。一切正常,但我的應用程序的主窗口的大小有問題。

當我打開一個普通大小的照片,我看到:

enter image description here

這是蠻好的。

但是當我打開另外一個文件,可以這樣說某事(一biiiiig圖的圖像):

enter image description here

我的應用程序窗口中有一個錯誤的寬度,我的意思是看看菜單的太緊了,你甚至沒有看到一個「編輯」選項。

如何解決?我剛開始在Python WX,所以,請耐心等待:)

我的代碼:

# -*- coding: utf-8 -*- 
#!/usr/bin/env python 

import wx 
import os 

class MyGUIApp(wx.App): 

    def __init__(self, redirect=False, filename=None): 

     wx.App.__init__(self, redirect, filename) 
     self.frame = wx.Frame(None, title='MyGUIApp v0.2') 
     self.panel = wx.Panel(self.frame) 

     self.filename = '' 
     self.dirname = '' 
     width, height = wx.DisplaySize() 
     self.pictureMaxSize = 500 

     img = wx.EmptyImage(self.pictureMaxSize, self.pictureMaxSize) 
     self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromImage(img)) 

     self.mainSizer = wx.BoxSizer(wx.VERTICAL) 
     self.mainSizer.Add(self.imageCtrl, 0, wx.ALL|wx.CENTER, 5) 
     self.panel.SetSizer(self.mainSizer) 
     self.mainSizer.Fit(self.frame) 

     self.createMenus() 
     self.connectItemsWithEvents() 
     self.createKeyboardShortcuts() 

     self.frame.SetMenuBar(self.menuBar) 
     self.frame.Show() 

    def connectItemsWithEvents(self) : 
     self.Bind(wx.EVT_MENU, self.openEvent, self.openItem) 
     self.Bind(wx.EVT_MENU, self.clearEvent, self.clearItem) 

    def createKeyboardShortcuts(self) : 
     self.accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL, ord('C'), self.clearItem.GetId()), 
              (wx.ACCEL_CTRL, ord('O'), self.openItem.GetId()), 
              ]) 
     self.frame.SetAcceleratorTable(self.accel_tbl) 

    def createMenus(self) : 
     self.menuBar = wx.MenuBar() 
     self.menuFile = wx.Menu() 

     self.menuBar.Append(self.menuFile, '&File')  
     self.openItem = wx.MenuItem(self.menuFile, wx.NewId(), u'&open ...\tCTRL+O') 
     #self.openItem.SetBitmap(wx.Bitmap('images/document-open.png')) 
     self.menuFile.AppendItem(self.openItem) 

     self.menuEdit = wx.Menu() 
     self.menuBar.Append(self.menuEdit, '&Edit') 
     self.clearItem = wx.MenuItem(self.menuEdit, wx.NewId(), '&Clear\tCTRL+C') 
     #self.clearItem.SetBitmap(wx.Bitmap('images/clear.png')) 
     self.menuEdit.AppendItem(self.clearItem) 


    def openEvent(self, event) : 
     openDialog = wx.FileDialog(self.frame, u'Open file', "File", "", "*.*", wx.OPEN) 
     if openDialog.ShowModal() == wx.ID_OK : 
      self.filename = openDialog.GetFilename() 
      self.dirname = openDialog.GetDirectory() 
      self.draw(os.path.join(self.dirname, self.filename)) 
     openDialog.Destroy() 

    def clearEvent(self, event) : 
     img = wx.EmptyImage(self.pictureMaxSize, self.pictureMaxSize) 
     self.imageCtrl = wx.StaticBitmap(self.panel, wx.ID_ANY, wx.BitmapFromImage(img)) 
     self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 
     self.frame.SetSize((self.pictureMaxSize, self.pictureMaxSize)) 
     self.filename = '' 
     self.dirname = '' 

    def draw(self, filename) : 
     image_name = filename 
     img = wx.Image(filename, wx.BITMAP_TYPE_ANY) 
     W = img.GetWidth() 
     H = img.GetHeight() 
     if W > H: 
      NewW = self.pictureMaxSize 
      NewH = self.pictureMaxSize * H/W 
     else: 
      NewH = self.pictureMaxSize 
      NewW = self.pictureMaxSize * W/H 
     img = img.Scale(NewW,NewH) 
     self.imageCtrl.SetBitmap(wx.BitmapFromImage(img)) 
     self.panel.Refresh() 
     self.mainSizer.Fit(self.frame) 

if __name__ == '__main__': 
    app = MyGUIApp() 
    app.MainLoop() 
+0

你期望什麼結果?你不明白你的'draw'方法的哪一部分? – phineas

+0

@phineas:當我打開一個尺寸非常大的圖像時,我會很喜歡這種圖像,我會看到滾動條或類似的圖像,以至於我可以看到我的整個應用程序gui,而不像我的第二個示例 – Katie

回答

1

你可以叫self.Frame.SetMinSize(W,H),以迫使窗口有一個合理的最小的高度和寬度,但你會想要添加滾動條,以便你可以看到所有的圖像...我試圖做到這一點,但我也是一個初學者,現在沒有時間,對不起。祝你好運!