如何在wxpython中使用wx.colorDialog刷新?
我正在尝试刷新()使用wx.colorDialog的面板。一旦我刷新面板一次,它将无法再次刷新。请尝试以下操作以查看实际问题 单击按钮,它将询问您希望将矩形更改为什么颜色。一旦你按下OK,它就会改变矩形的颜色。它不会工作,也不会更改矩形如何在wxpython中使用wx.colorDialog刷新?,python,wxpython,wxwidgets,Python,Wxpython,Wxwidgets,我正在尝试刷新()使用wx.colorDialog的面板。一旦我刷新面板一次,它将无法再次刷新。请尝试以下操作以查看实际问题 单击按钮,它将询问您希望将矩形更改为什么颜色。一旦你按下OK,它就会改变矩形的颜色。它不会工作,也不会更改矩形 import wx xcolor_of_font_dia=(0,0,0) class MyFrame(wx.Frame): """a frame with a panel""" def __init__(self, parent=None,
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()
我清理了一下你的代码。基本上,当您在每次大小事件之后创建(和删除)不同的dc实例时,您的globals都会产生一些问题。
如果globals不是严格必需的(很少需要) 这项工作:
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()
为我工作(TM)。不得不添加一个
dc.SetBrush(wx.Brush(xcolor\u of\u font\u dia))
使它更明显,我想。我连续几次改变了颜色。矩形每次都变右。@RicardoCárdenes它起作用了吗?在我的情况下,我得到了完全相同的行为描述的OP。第一次工作,不再。这是ActivePython2.6中的win 7 64位wxpython 2.8.12.1。7@joaquin:是的,它确实起作用了。相同的wxPython版本,但是Linux和Python 2.7.2。代码没有变化(除了填充矩形)。由于wx在每个平台上都使用本机小部件库,因此问题可能只在Windows上出现。@RicardoCárdenes这里,如果将dc设置为全局,则帧似乎进入了一个非常快速、永无止境的循环,重新绘制矩形。我看到边框在颤抖(我没有填充矩形,而是用wx.Pen使边框变厚)。按钮仍然有效,但颜色对话框对象(尽管已创建)不再显示。奇怪。。。