如何在wxpython中使用Refresh和wx.ColourDialog?

发布于 2024-12-25 06:24:21 字数 1731 浏览 0 评论 0原文

我正在尝试刷新()使用 wx.ColourDialog 的面板。一旦我刷新面板一次,就无法再次刷新。请尝试执行以下操作来查看问题的实际情况。

单击该按钮,它会询问您要将矩形更改为什么颜色。一旦你按确定,它应该改变矩形的颜色。它不会工作,不会改变矩形。

import wx 
xcolor_of_font_dia=(0,0,0)
class MyFrame(wx.Frame): 
    """a frame with a panel"""
    def __init__(self, parent=None, id=wx.ID_ANY, title=None):
        global xcolor_of_font_dia
        global dc
        wx.Frame.__init__(self, parent, wx.ID_ANY, title) 
        self.panel = wx.Panel(self, size=(350, 200)) 
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
        self.button2 = wx.Button(self.panel, id=wx.ID_ANY, label='Button2',pos=(8, 38), size=(175, 28))
        self.button2.Bind(wx.EVT_BUTTON, self.onColorDlg)
        self.Fit() 
    def onColorDlg(self, event):
        global xcolor_of_font_dia
        global dc
        """
        This is mostly from the wxPython Demo!
        """
        dlg = wx.ColourDialog(self)

        # Ensure the full colour dialog is displayed, 
        # not the abbreviated version.
        dlg.GetColourData().SetChooseFull(True)

        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetColourData()
            print 'You selected: %s\n' % str(data.GetColour().Get())
            xcolor_of_font_dia='#%02x%02x%02x' % data.GetColour().Get()
        dlg.Destroy()
        self.panel.Refresh()
    def on_paint(self, event):
        global xcolor_of_font_dia
        global dc
        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen(xcolor_of_font_dia, 1))
        rect = wx.Rect(50, 50, 100, 100) 
        dc.DrawRoundedRectangleRect(rect, 8)


# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame(title='rounded-rectangle & circle') 
frame1.Center() 
frame1.Show() 
app.MainLoop()

i am trying to Refresh() a panel which uses the wx.ColourDialog. Once I refresh the panel once, it is unable to refresh again. Try the following to see the problem in action.

By clicking the button, it will ask you what color you would like to change the rectangle to. Once you press OK, it should change the rectangles color. It will not work it will not change the rectangle.

import wx 
xcolor_of_font_dia=(0,0,0)
class MyFrame(wx.Frame): 
    """a frame with a panel"""
    def __init__(self, parent=None, id=wx.ID_ANY, title=None):
        global xcolor_of_font_dia
        global dc
        wx.Frame.__init__(self, parent, wx.ID_ANY, title) 
        self.panel = wx.Panel(self, size=(350, 200)) 
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
        self.button2 = wx.Button(self.panel, id=wx.ID_ANY, label='Button2',pos=(8, 38), size=(175, 28))
        self.button2.Bind(wx.EVT_BUTTON, self.onColorDlg)
        self.Fit() 
    def onColorDlg(self, event):
        global xcolor_of_font_dia
        global dc
        """
        This is mostly from the wxPython Demo!
        """
        dlg = wx.ColourDialog(self)

        # Ensure the full colour dialog is displayed, 
        # not the abbreviated version.
        dlg.GetColourData().SetChooseFull(True)

        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetColourData()
            print 'You selected: %s\n' % str(data.GetColour().Get())
            xcolor_of_font_dia='#%02x%02x%02x' % data.GetColour().Get()
        dlg.Destroy()
        self.panel.Refresh()
    def on_paint(self, event):
        global xcolor_of_font_dia
        global dc
        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen(xcolor_of_font_dia, 1))
        rect = wx.Rect(50, 50, 100, 100) 
        dc.DrawRoundedRectangleRect(rect, 8)


# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame(title='rounded-rectangle & circle') 
frame1.Center() 
frame1.Show() 
app.MainLoop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筑梦 2025-01-01 06:24:21

我清理了一下你的代码。基本上,当您在每个大小事件后创建(和删除)不同的 dc 实例时,您的全局变量会产生一些问题。
如果不是绝对必要(很少是),则不应使用全局变量。

这有效:

import wx 


class MyFrame(wx.Frame): 
    """a frame with a panel"""
    def __init__(self, parent=None, id=wx.ID_ANY, title=None):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title) 
        self.xcolor = (0, 0, 0)
        self.panel = wx.Panel(self, size=(350, 200)) 
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
        self.button2 = wx.Button(self.panel, id=wx.ID_ANY, label='Button2',
                                             pos=(8, 38), size=(175, 28))
        self.button2.Bind(wx.EVT_BUTTON, self.onColorDlg)

        self.Fit() 

    def onColorDlg(self, event):
        """
        This is mostly from the wxPython Demo!
        """
        dlg = wx.ColourDialog(None)
        dlg.GetColourData().SetChooseFull(True)

        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetColourData()
            self.xcolor = data.GetColour().Get()
            print 'You selected: %s\n' % str(self.xcolor)

        dlg.Destroy()
        self.panel.Refresh()

    def on_paint(self, event):
        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen(self.xcolor, 2))
        rect = wx.Rect(50, 50, 100, 100) 
        dc.DrawRoundedRectangleRect(rect, 8)


# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame(title='rounded-rectangle & circle') 
frame1.Center() 
frame1.Show() 
app.MainLoop()

I cleaned your code a bit. Basically your globals were producing some problems as you were creating (and deleting) different dc instances after every size event.
You should not use globals if it is not strictly necessary (rarely is).

This works:

import wx 


class MyFrame(wx.Frame): 
    """a frame with a panel"""
    def __init__(self, parent=None, id=wx.ID_ANY, title=None):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title) 
        self.xcolor = (0, 0, 0)
        self.panel = wx.Panel(self, size=(350, 200)) 
        self.panel.Bind(wx.EVT_PAINT, self.on_paint)
        self.button2 = wx.Button(self.panel, id=wx.ID_ANY, label='Button2',
                                             pos=(8, 38), size=(175, 28))
        self.button2.Bind(wx.EVT_BUTTON, self.onColorDlg)

        self.Fit() 

    def onColorDlg(self, event):
        """
        This is mostly from the wxPython Demo!
        """
        dlg = wx.ColourDialog(None)
        dlg.GetColourData().SetChooseFull(True)

        if dlg.ShowModal() == wx.ID_OK:
            data = dlg.GetColourData()
            self.xcolor = data.GetColour().Get()
            print 'You selected: %s\n' % str(self.xcolor)

        dlg.Destroy()
        self.panel.Refresh()

    def on_paint(self, event):
        dc = wx.PaintDC(self.panel)
        dc.SetPen(wx.Pen(self.xcolor, 2))
        rect = wx.Rect(50, 50, 100, 100) 
        dc.DrawRoundedRectangleRect(rect, 8)


# test it ...
app = wx.PySimpleApp() 
frame1 = MyFrame(title='rounded-rectangle & circle') 
frame1.Center() 
frame1.Show() 
app.MainLoop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文