当通过按 wxPython 中的“取消”按钮放弃操作时,如何避免按钮保留“选定”状态?

问题描述 投票:0回答:1

我有一个代码,每个按钮上都有一些按钮和一些关联的“确定/取消”对话框。整个代码正确地完成了它应该做的事情,但是有一个我不知道如何避免/处理的视觉故障:按钮的悬停“选择”状态在点击“取消”按钮操作后继续保持选中状态。

为了举例说明,我制作了真实代码的精简测试代码,如下所示,它在某种程度上模仿了原始程序结构(也有工具栏、状态栏等)。

只需运行代码并按照右侧窗格中显示的提示进行操作即可。警告对话框上的文本只是专门为此测试而制作的哑文本。

问题:点击取消按钮后如何恢复原来的按钮颜色? (鼠标悬停除外)

编辑:这是在 Windows 操作系统上。

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import wx

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((438, 300))
        self.SetTitle("frame")
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        self.window_1 = wx.SplitterWindow(self, wx.ID_ANY)
        self.window_1.SetMinimumPaneSize(20)
        sizer_1.Add(self.window_1, 1, wx.EXPAND, 0)
        self.window_1_pane_1 = wx.Panel(self.window_1, wx.ID_ANY)
        sizer_2 = wx.BoxSizer(wx.VERTICAL)
        grid_sizer_1 = wx.GridSizer(1, 2, 0, 0)
        sizer_2.Add(grid_sizer_1, 1, wx.EXPAND, 0)

        self.button_1 = wx.Button(self.window_1_pane_1, wx.ID_ANY, "button_1")
        self.button_1.SetMinSize((75, 75))
        self.button_1.SetBackgroundColour(wx.Colour(0x66, 0x77, 0x88))
        self.button_1.SetForegroundColour(wx.Colour(0xcc, 0xcc, 0xcc))
        grid_sizer_1.Add(self.button_1, 0, wx.ALIGN_CENTER, 0)

        self.button_2 = wx.Button(self.window_1_pane_1, wx.ID_ANY, "button_2")
        self.button_2.SetMinSize((75, 75))
        self.button_2.SetBackgroundColour(wx.Colour(0x66, 0x77, 0x88))
        self.button_2.SetForegroundColour(wx.Colour(0xcc, 0xcc, 0xcc))
        grid_sizer_1.Add(self.button_2, 0, wx.ALIGN_CENTER, 0)

        self.window_1_pane_2 = wx.Panel(self.window_1, wx.ID_ANY)
        sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_4 = wx.BoxSizer(wx.VERTICAL)
        sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)

        label_1 = wx.StaticText(self.window_1_pane_2, wx.ID_ANY,
            "Change focus to the button below with the Tab key on keyboard, "\
            "then click one of the buttons on the left and then hit Cancel.\n"\
            "To restore the buttons's 'selected' status, just hover the mouse over it.",
            style=wx.ALIGN_CENTER_HORIZONTAL)
        sizer_4.Add(label_1, 1, wx.EXPAND, 0)
        self.button_3 = wx.Button(self.window_1_pane_2, wx.ID_ANY, "reset color")
        sizer_4.Add(self.button_3, 1, wx.EXPAND, 0)

        self.window_1_pane_2.SetSizer(sizer_3)
        self.window_1_pane_1.SetSizer(sizer_2)
        self.window_1.SplitVertically(self.window_1_pane_1, self.window_1_pane_2)
        self.SetSizer(sizer_1)
        self.Layout()

        self.button_1.Bind(wx.EVT_LEFT_DOWN, self.change_button_1)
        self.button_2.Bind(wx.EVT_LEFT_DOWN, self.change_button_2)
        self.button_3.Bind(wx.EVT_LEFT_DOWN, self.reset_color)

    def change_button_1(self, event):
        self.dlg = wx.MessageDialog(None,
        "CAUTION\n\nYou are about to chage button 1 color!",
        "Color change warning", wx.OK | wx.CANCEL | wx.ICON_WARNING)
        self.dlg_result = self.dlg.ShowModal()
        self.dlg.Destroy()

        if self.dlg_result == wx.ID_OK:
            self.button_1.SetBackgroundColour(wx.Colour(0xcc, 0x32, 0x32))
            self.button_2.SetBackgroundColour(wx.Colour(0x66, 0x77, 0x88))

    def change_button_2(self, event):
        self.dlg = wx.MessageDialog(None,
        "CAUTION\n\nYou are about to chage button 2 color!",
        "Color change warning", wx.OK | wx.CANCEL | wx.ICON_WARNING)
        self.dlg_result = self.dlg.ShowModal()
        self.dlg.Destroy()

        if self.dlg_result == wx.ID_OK:
            self.button_2.SetBackgroundColour(wx.Colour(0xcc, 0x32, 0x32))
            self.button_1.SetBackgroundColour(wx.Colour(0x66, 0x77, 0x88))

    def reset_color(self, event):
        self.button_2.SetBackgroundColour(wx.Colour(0x66, 0x77, 0x88))
        self.button_1.SetBackgroundColour(wx.Colour(0x66, 0x77, 0x88))

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()
wxpython
1个回答
0
投票

我刚刚在 Python 3.6 上尝试使用 wxPython 4.1,事情似乎按预期工作。

所以,您的问题可能是版本特定的错误。

您使用什么版本?

您尝试打电话给

self.button_2.Refresh()
吗? 您还可以尝试在事件处理程序中调用
event.Skip()

© www.soinside.com 2019 - 2024. All rights reserved.