wxPython用于图像和按钮(可调整大小) [英] wxPython for image and buttons (resizable)

查看:1791
本文介绍了wxPython用于图像和按钮(可调整大小)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把这样的图像放在 wx.Panel 中:




  • 动物应该是按钮,这样如果我点击它们,它们的图像会发生变化,如果我重新点击,图像会恢复正常(因此动物可以被认为是简单的 BitmapToggleButtons ,正如另一个问题所建议的那样)


  • 此面板应调整大小/重新调整(所有子图像/ togglebuttons也是如此) !)保持纵横比,如果父 wx.Panel 被调整为更小的东西,例如(就像标准 Windows Photo Viewer 那样: ,但现在我完全不知道如何继续(检测点击,用直接DC绘画更新按钮?),这就是赏金的原因。

    解决方案

    我用以下方法解决了问题: / p>

     从floatcanvas导入导入wx 
    FloatCanvas

    类MyPanel(wx.Panel):
    def __init __(self,parent):
    super(MyPanel,self).__ init __(parent)
    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.SetSizer( self.sizer)

    #添加画布
    self.Canvas = FloatCanvas.FloatCanvas(self,BackgroundColor =LIGHT GRAY)
    self.Canvas.Bind(wx.EVT_SIZE,self.OnSize)
    self.sizer.Add(self.Canvas,-1,flag = wx.EXPAND)

    #添加一个切换按钮
    image_dis = wx.Image('file_disabled.png')
    image_ena = wx.Image('file_enabled.png')
    img_dis = self。 Canvas.AddScaledBitmap(image_dis,(x,-y),Height = image_dis.GetHeight(),Position ='tl')
    img_ena = self.Canvas.AddScaledBitmap(image_ena,(x,-y),Height = image_ena.GetHeight(),Position ='tl')
    img_dis.other = img_ena
    img_ena.other = img_dis
    img_ena.Visible = False

    #bind切换按钮事件
    img_dis.Bind(FloatCanvas.EVT_FC_LEFT_UP,self.OnToggle)
    img_ena.Bind(FloatCanvas.EVT_FC_LEFT_UP,self.OnToggle)

    def OnToggle(self,button) :
    button.other.Visible = True
    button.Visible = False
    self.Canvas.Draw(True)

    def OnSize(self,event):
    event.Skip()
    wx.CallLater(1,self.Canvas.ZoomToBB)


    I want to put such an image in a wx.Panel :

    • The animals should be "buttons" so that if I click on them, their image changes, and if I reclick, the image returns to normal (thus the animals can be considered as simple BitmapToggleButtons, as suggested by another question here on SO)

    • This panel should be resized/rescaled (all all the children images / togglebuttons too!) keeping the aspect ratio, if the parent wx.Panel is resized to something smaller for example (like would do the standard Windows Photo Viewer : http://res1.windows.microsoft.com/resbox/en/windows%207/main/7eaf462a-86dd-42d2-a789-7413f5472dae_63.jpg)

    I am still a bit lost on : how to implement such a clickable (with toggle buttons) and rescalable Canvas?

    Edit : I started with something fruitful here Rescale image when parent is resized in wxPython, but now I'm totally stuck about how to continue (detect clicks, update buttons with direct DC Painting ?), that's why the bounty.

    解决方案

    I solved the problem with :

    import wx
    from floatcanvas import FloatCanvas
    
    class MyPanel(wx.Panel):
        def __init__(self, parent):
            super(MyPanel, self).__init__(parent)
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.SetSizer(self.sizer)
    
            # add a canvas
            self.Canvas = FloatCanvas.FloatCanvas(self, BackgroundColor = "LIGHT GREY")
            self.Canvas.Bind(wx.EVT_SIZE, self.OnSize)
            self.sizer.Add(self.Canvas, -1, flag=wx.EXPAND)
    
            # add a toggle button
            image_dis = wx.Image('file_disabled.png')
            image_ena = wx.Image('file_enabled.png')
            img_dis = self.Canvas.AddScaledBitmap(image_dis, (x,-y), Height=image_dis.GetHeight(), Position = 'tl')
            img_ena = self.Canvas.AddScaledBitmap(image_ena, (x,-y), Height=image_ena.GetHeight(), Position = 'tl')
            img_dis.other = img_ena
            img_ena.other = img_dis
            img_ena.Visible = False
    
            # bind the toggle button event 
            img_dis.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.OnToggle)
            img_ena.Bind(FloatCanvas.EVT_FC_LEFT_UP, self.OnToggle)
    
        def OnToggle(self, button):
            button.other.Visible = True
            button.Visible = False
            self.Canvas.Draw(True)
    
        def OnSize(self, event):
            event.Skip()
            wx.CallLater(1, self.Canvas.ZoomToBB)  
    

    这篇关于wxPython用于图像和按钮(可调整大小)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆