wxPython如何制作动态滚动面板

时间:2017-12-25 12:23:15

标签: python wxpython

我遇到了问题:我想为自己制作密码管理器。

我有完成en / decode的逻辑,并且我已经完成了动态添加密码,但是如果我向管理员添加太多密码,那么我看不到所有密码,因为我的屏幕太小了

我想添加一个滚动条,但滚动条不起作用。 即使经过几个小时的研究,代码也不想工作。这是PanelTwo的代码:

class PanelTwo(wx.Panel):
@staticmethod
def scale_bitmap(bitmap, width, height):
    image = wx.ImageFromBitmap(bitmap)
    image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
    result = wx.BitmapFromImage(image)
    return result

def __init__(self, parent):
    wx.Panel.__init__(self, parent=parent)

    self.refr = wx.Button(self,wx.ID_ANY,u"Refresh")
    self.refr.Bind(wx.EVT_BUTTON, self.refresh)

    self.co = wx.Button(self, wx.ID_ANY, u"Close")
    self.co.Bind(wx.EVT_BUTTON, self.close)

    self.number_of_pwds = 0
    self.frame = parent

    self.mainSizer = wx.BoxSizer(wx.VERTICAL)
    controlSizer = wx.BoxSizer(wx.HORIZONTAL)
    self.widgetSizer = wx.BoxSizer(wx.VERTICAL)

    #controlSizer.Add(self.Passest, 0, wx.CENTER | wx.ALL, 5)
    controlSizer.Add(self.refr, 0, wx.CENTER | wx.ALL, 5)
    controlSizer.Add(self.co, 0, wx.CENTER | wx.ALL, 5)

    self.addButton = wx.Button(self, label="Add")
    self.addButton.Bind(wx.EVT_BUTTON, self.onAddWidget)
    controlSizer.Add(self.addButton, 0, wx.CENTER | wx.ALL, 5)

    self.removeButton = wx.Button(self, label="Remove")
    self.removeButton.Bind(wx.EVT_BUTTON, self.onRemoveWidget)
    controlSizer.Add(self.removeButton, 0, wx.CENTER | wx.ALL, 5)

    self.mainSizer.Add(controlSizer, 0, wx.CENTER)
    self.mainSizer.Add(self.widgetSizer, 0, wx.CENTER | wx.ALL, 10)

    self.SetSizer(self.mainSizer)

Pwds的添加和删除在这里:

def onAddWidget(self, event):
    self.number_of_pwds += 1
    label = "Pwd %s" % self.number_of_pwds
    name = "Pwd%s" % self.number_of_pwds
    new_Text = wx.StaticText(self, label=label, name=name)
    self.widgetSizer.Add(new_Text, 0, wx.ALL, 5)
    self.frame.fSizer.Layout()
    self.frame.Fit()


def onRemoveWidget(self, event):
    if self.widgetSizer.GetChildren():
        self.widgetSizer.Hide(self.number_of_pwds - 1)
        self.widgetSizer.Remove(self.number_of_pwds - 1)
        self.number_of_pwds -= 1
        self.frame.fSizer.Layout()
        self.frame.Fit()

我的主要表格在这里:

class MyForm(wx.Frame):
def __init__(self):
    wx.Frame.__init__(self, None, wx.ID_ANY,
                      "Passwort Manager",
                      size=(300,130))
    self.panel_one = PanelOne(self)
    self.panel_two = PanelTwo(self)
    self.panel_three = PanelThree(self)
    self.panel_two.Hide()
    self.panel_three.Hide()

    self.fSizer = wx.BoxSizer(wx.VERTICAL)
    self.fSizer.Add(self.panel_one, 1, wx.EXPAND)
    self.fSizer.Add(self.panel_two, 1, wx.EXPAND)
    self.fSizer.Add(self.panel_three,1,wx.EXPAND)
    self.SetSizer(self.fSizer)
    self.SetBackgroundColour(Color.White)


def onSwitchPanels(self, event):

    if self.panel_one.IsShown():
       self.SetTitle("Passwort Manager")
       self.SetBackgroundColour(Color.Random())
       self.panel_one.Hide()
       self.panel_two.Show()
    elif self.panel_two.IsShown():
       self.SetTitle("Passwort Manager")
       self.SetBackgroundColour(Color.Random())
       self.panel_three.Show()
       self.panel_two.Hide()
    else:
        self.SetTitle("Passwort Manager")
        self.SetBackgroundColour(Color.Random())
        self.panel_one.Show()
        self.panel_three.Hide()
    self.Layout()

app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

那么如何添加动态滚动条,该滚动条会自动调整大小并与我的代码一起使用?我使用的是Python3.6。

感谢您帮助我。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用滚动面板?

import wx
import wx.lib.scrolledpanel as scrolled

class TestPanel(scrolled.ScrolledPanel):

    def __init__(self, parent):

        scrolled.ScrolledPanel.__init__(self, parent, -1)

        """
        Write your code here...
        """

        self.SetupScrolling()