简体   繁体   English

wxPython如何制作动态滚动面板

[英]wxPython How to make a dynamic scrolled panel

I have a problem: I want to make a password manager for myself . 我有一个问题:我想为自己创建一个密码管理器。

I have the logic for the en/decoding done and also I have the dynamic adding of passwords done, but if I add too many passwords to my manager then I can't see all of the passwords because my screen is too small. 我完成了编码/解码的逻辑,还完成了动态添加密码的操作,但是如果我向管理员添加了太多密码,则由于屏幕太小,我将看不到所有密码。

I wanted to add a scroll bar but the scroll bar didn't work. 我想添加滚动条,但是滚动条不起作用。 Even after several hours of researching the code didn't want to work. 即使经过数小时的研究,该代码也无法正常工作。 This is the code for the PanelTwo: 这是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)

The adding and removing of the Pwds is here : 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()

my main Form is here : 我的主要表格在这里:

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

So how can I add a dynamic scrollbar that resizes automatically and that works with my code? 那么,如何添加一个自动调整大小并与我的代码一起使用的动态滚动条呢? I am using Python3.6. 我正在使用Python3.6。

Thanks for helping me out. 谢谢你的协助。

Have you tried using a scrolled panel? 您是否尝试过使用滚动面板?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM