I have a code with some buttons and some associated OK/Cancel dialog on each. The overall code does properly what it should, but there is a visual glitch that I don't know how to avoid/handle: the hovering 'select' status of a button continue to remain selected after hitting its Cancel button action.
我有一个带有一些按钮的代码,每个按钮上都有一些相关的确定/取消对话框。整体代码做了它应该做的事情,但有一个视觉故障,我不知道如何避免/处理:按钮的悬停‘选择’状态在点击它的取消按钮操作后继续保持选中状态。
In order to exemplify, I made a stripped down test code of my real one, shown below, that mimics somewhat the original program structure (which also has toolbar, status bar etc.).
为了举例说明,我对我的真实代码做了一个精简的测试代码,如下所示,它在某种程度上模仿了原始的程序结构(它也有工具栏、状态栏等)。
Just run the code and follow the hint displayed on the right pane. The text on the warning dialog is just a dumb text made only for this test.
只需运行代码并按照右窗格上显示的提示进行操作。警告对话框上的文本只是一个仅用于此测试的哑文本。
Question: how to restore tyhe original button color after hitting the Cancel button? (except with the mouse over hovering)
问:按下取消按钮后如何恢复原来的按钮颜色?(鼠标悬停在上方时除外)
Edit: this is on Windows OS.
编辑:这是在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()
更多回答
I have just tried with wxPython 4.1 on Python 3.6 and things seem to work as expected.
我刚刚在Python3.6上尝试了wxPython4.1,似乎运行正常。
So, your problem could be a version specific bug.
因此,您的问题可能是特定于版本的错误。
What versions are you using?
你用的是什么版本?
Did you try to call self.button_2.Refresh()
?
You could also try to call event.Skip()
in your event handler.
您有没有尝试调用self.Button_2.Refresh()?您还可以尝试在事件处理程序中调用vent.Skip()。
更多回答
I am using wx.version \'4.2.1 msw (phoenix) wxWidgets 3.2.2.1\' and Python 3.11.3. The self.button_2.Refresh()
method solved the issue. Thank you :)
我使用的是wx.version4.2.1 MSW(Phoenix)wxWidget 3.2.2.1\‘和Python3.11.3。Self.Button_2.Refresh()方法解决了这个问题。谢谢:)
我是一名优秀的程序员,十分优秀!