gpt4 book ai didi

python - Wxpython 图像查看器 - 打开图像文件后菜单太紧

转载 作者:行者123 更新时间:2023-11-28 17:49:39 29 4
gpt4 key购买 nike

我在 Ubuntu 12.04 上使用 Python 2.7 和 wx。

我用 Python 使用 wx.js 编写了一个非常非常小的图像查看器。一切都很好,但我的应用程序主窗口的大小有问题。

当我打开一张普通尺寸的图片时,我看到:

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()

最佳答案

您可以调用 self.Frame.SetMinSize(w, h) 强制窗口具有合理的最小高度和宽度,但您需要添加滚动条以便可以看到所有图像...我试过了这样做,但我也是初学者,目前没有时间,抱歉。祝你好运!

关于python - Wxpython 图像查看器 - 打开图像文件后菜单太紧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12652052/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com