繁体   English   中英

WxPython,“滚动面板”中的按钮处于非活动状态(不可单击)

[英]WxPython, buttons in Scrolled Panel inactive (not clickable)

我有一个在滚动面板中创建按钮的程序:

import wx
import wx.lib.scrolledpanel as scrolled


class EditWindow(wx.Frame):

    def __init__(self, *args, **kwargs):
        self.width = 400
        self.height = 370
        wx.Frame.__init__(self, size=(self.width, self.height), style=wx.MINIMIZE_BOX | wx.CLOSE_BOX | wx.CAPTION,
                      *args, **kwargs)
        self.GUI()
        self.Show(True)

    def GUI(self):
        button_dict = {}
        global button_dict

        divisor = 120

        panel = wx.Panel(self, -1, pos=(0, 0), size=(self.width, 400))

        ok_button = wx.Button(panel, -1, label='Ok', pos=((self.width - 80) / 2, 80))
        ok_button.SetSize((80, 25))
        ok_button.Bind(wx.EVT_BUTTON, self.period_select)

        scrolled_panel = scrolled.ScrolledPanel(self, -1, pos=(140, divisor),
                                            size=(self.width - 285, 200),
                                            style=wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER, name='')
        scrolled_panel.SetupScrolling()

        spSizer = wx.BoxSizer(wx.VERTICAL)
        scrolled_panel.SetSizer(spSizer)
        global spSizer

        panelSizer = wx.BoxSizer(wx.VERTICAL)

        global panel, panelSizer, scrolled_panel, spSizer

    def period_select(self, event):
        global scrolled_panel, spSizer, button_dict
        button_dict = {}

        for i in range(1, 4):
            button_dict[str(i)] = wx.Button(scrolled_panel,
                                             label='{0}'.format(i))
            button_dict[str(i)].Bind(wx.EVT_BUTTON, self.printa)
            spSizer.Add(button_dict[str(i)])

        scrolled_panel.Layout()
        scrolled_panel.SetupScrolling()
        panel.SetSizer(panelSizer)

    def printa(self, event):
        print('a')


def main():
    app = wx.App()
    EditWindow(None)
    app.MainLoop()

main()

问题是:此代码在滚动面板中正确创建了Frame和按钮,但是当我尝试单击按钮时,它们似乎根本不起作用。 另外,滚动条会正确显示,但不能使用(用鼠标滚轮或单击并拖动)。 我究竟做错了什么?

- 编辑 -

这是我在PyCharm中运行代码时收到的消息:

C:\Python34\python.exe C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py
C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py:17: SyntaxWarning: name 'button_dict' is assigned to before global declaration
  global button_dict
C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py:34: SyntaxWarning: name 'spSizer' is assigned to before global declaration
  global spSizer
C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py:38: SyntaxWarning: name 'panel' is assigned to before global declaration
  global panel, panelSizer, scrolled_panel, spSizer
C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py:38: SyntaxWarning: name 'panelSizer' is assigned to before global declaration
  global panel, panelSizer, scrolled_panel, spSizer
C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py:38: SyntaxWarning: name 'scrolled_panel' is assigned to before global declaration
  global panel, panelSizer, scrolled_panel, spSizer
C:/Users/USER/PycharmProjects/PokerissimoStats/SE.py:38: SyntaxWarning: name 'spSizer' is assigned to before global declaration
  global panel, panelSizer, scrolled_panel, spSizer

-编辑2--

我重写了代码以摆脱SyntaxWarnings,但问题仍然存在

import wx
import wx.lib.scrolledpanel as scrolled


class EditWindow(wx.Frame):

    def __init__(self, *args, **kwargs):
        self.width = 400
        self.height = 370
        wx.Frame.__init__(self, size=(self.width, self.height), style=wx.MINIMIZE_BOX | wx.CLOSE_BOX | wx.CAPTION,
                      *args, **kwargs)
        self.GUI()
        self.Show(True)

    def GUI(self):

        global button_dict, spSizer, panel, panelSizer, scrolled_panel
        button_dict = {}

        divisor = 120

        panel = wx.Panel(self, -1, pos=(0, 0), size=(self.width, 400))

        ok_button = wx.Button(panel, -1, label='Ok', pos=((self.width - 80) / 2, 80))
        ok_button.SetSize((80, 25))
        ok_button.Bind(wx.EVT_BUTTON, self.period_select)

        scrolled_panel = scrolled.ScrolledPanel(self, -1, pos=(140, divisor),
                                            size=(self.width - 285, 200),
                                            style=wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER, name='')
        scrolled_panel.SetupScrolling()

        spSizer = wx.BoxSizer(wx.VERTICAL)
        scrolled_panel.SetSizer(spSizer)

        panelSizer = wx.BoxSizer(wx.VERTICAL)

    def period_select(self, event):
        global scrolled_panel, spSizer, button_dict
        button_dict = {}

        for i in range(1, 4):
            button_dict[str(i)] = wx.Button(scrolled_panel,
                                             label='{0}'.format(i))
            button_dict[str(i)].Bind(wx.EVT_BUTTON, self.printa)
            spSizer.Add(button_dict[str(i)])

        scrolled_panel.Layout()
        scrolled_panel.SetupScrolling()
        panel.SetSizer(panelSizer)

    def printa(self, event):
        print('a')


def main():
    app = wx.App()
    EditWindow(None)
    app.MainLoop()

main()

转储所有全局变量并改用self。 这根本不是一个答案,但是我已经整理了一下代码,以便从命令行运行(Linux wxpython 3.0)

import wx
import wx.lib.scrolledpanel as scrolled


class EditWindow(wx.Frame):

    def __init__(self, *args, **kwargs):
        self.width = 400
        self.height = 370
        wx.Frame.__init__(self, size=(self.width, self.height), style=wx.MINIMIZE_BOX | wx.CLOSE_BOX | wx.CAPTION,
                      *args, **kwargs)
        divisor = 120

        self.panel = wx.Panel(self, -1, pos=(0, 0), size=(self.width, 400))

        ok_button = wx.Button(self.panel, -1, label='Ok', pos=((self.width - 80) / 2, 80))
        ok_button.SetSize((80, 25))
        ok_button.Bind(wx.EVT_BUTTON, self.period_select)

        self.scrolled_panel = scrolled.ScrolledPanel(self.panel, -1, pos=(140, divisor),
                                            size=(self.width - 285, 200),
                                            style=wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER, name='')
        self.spSizer = wx.BoxSizer(wx.VERTICAL)
        self.scrolled_panel.SetSizer(self.spSizer)
        self.panelSizer = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(self.panelSizer)

        self.Show()

    def period_select(self, event):
        button_dict = {}

        for i in range(1, 4):
            button_dict[str(i)] = wx.Button(self.scrolled_panel,label='{0}'.format(i))
            button_dict[str(i)].Bind(wx.EVT_BUTTON, self.printa)
            self.spSizer.Add(button_dict[str(i)])
        self.scrolled_panel.SetupScrolling()
        self.scrolled_panel.Layout()

    def printa(self, event):
        print('a')


def main():
    app = wx.App()
    EditWindow(None)
    app.MainLoop()

main()

暂无
暂无

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

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