wx.Choice mit farblichen Hintergrund
Verfasst: Freitag 28. Oktober 2005, 17:42
Hallöche,
ich habe da so ein kleines prob, ich habe ein ganz normales wx.Choice Element und möchte, dass die Einträge einen farbigen Hintergrund haben (jeder einen anderen !!!)... wi mache ich so etwas??? und noch als zweites, gibt es eine funktion, die alle Farben, die wxPython durch namen erkennt (BSP: 'Forest Green'),ausgibt????
also schon ma danke im voraus
PS: ich benutze windows, falls das relevant ist ....
hier übrigens mal mein Code (die Farben sollen natürlich in die Config):
PS: ist ein wenig unordentlich, aber ich hoffe mal, man knn es lesen ...
ich habe da so ein kleines prob, ich habe ein ganz normales wx.Choice Element und möchte, dass die Einträge einen farbigen Hintergrund haben (jeder einen anderen !!!)... wi mache ich so etwas??? und noch als zweites, gibt es eine funktion, die alle Farben, die wxPython durch namen erkennt (BSP: 'Forest Green'),ausgibt????
also schon ma danke im voraus
PS: ich benutze windows, falls das relevant ist ....
hier übrigens mal mein Code (die Farben sollen natürlich in die Config):
Code: Alles auswählen
# -*- coding: cp1252 -*-
import wx
from math import *
ALL_COLORS=[
'Black',
'White',
'Yellow',
'Red',
'Green',
'Blue',
'Purple',
'Brown',
'Aquamarine',
'Forest Green',
'Light Blue',
'Goldenrod',
'Cyan',
'Orange',
'Navy',
'Dark Grey',
'Light Grey'
]
COLOR={
'bg':'White',
'skale':'Black',
'graph':'Forest Green'
}
DEUTSCH={
"title":"Funktionsanzeige",
'file':'Datei',
'new':'Neu',
'new status bar':'Zeichnet eine neue Funktion',
'exit':'Beenden',
'exit status bar':'Beendet das Programm',
'edit':'Bearbeiten',
'config':'Einstellungen',
'config status bar':'Konfigurieren des Programms',
'quest exit':'Wollen Sie wirklich beenden?',
'exit title':'Beenden ...',
'config title':'Einstellungen',
'config x':'x-Anzeige',
'config y':'y-Anzeige',
'config graph color':'Graphfarbe',
'config bg color':'Hintergrundfarbe',
'config skale color':'Skalenfarbe'
}
class config_dlg(wx.Dialog):
def __init__(self,parent,id,dic,x,y,color):
pre = wx.PreDialog()
pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
pre.Create(parent, id, dic['config title'], wx.DefaultPosition, wx.DefaultSize, wx.DEFAULT_DIALOG_STYLE)
self.PostCreate(pre)
sizer = wx.BoxSizer(wx.VERTICAL)
label = wx.StaticText(self, -1, "Hier können Sie die Einstellungen ändern.")
sizer.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
label = wx.StaticText(self, -1, dic['config x'])
box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
self.x = wx.TextCtrl(self, -1, "%i"%x, size=(80,-1))
box.Add(self.x, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box = wx.BoxSizer(wx.HORIZONTAL)
label = wx.StaticText(self, -1, dic['config y'])
box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
self.y = wx.TextCtrl(self, -1, "%i"%y, size=(80,-1))
box.Add(self.y, 1, wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
box=wx.BoxSizer(wx.HORIZONTAL)
label=wx.StaticText(self,-1,dic['config bg color'])
box.Add(label,1,wx.ALIGN_CENTRE|wx.ALL, 5)
self.bg=wx.Choice(self,-1,(100, 50),choices = ALL_COLORS)
self.bg.Select(ALL_COLORS.index(color['bg']))
box.Add(self.bg,0,wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box=wx.BoxSizer(wx.HORIZONTAL)
label=wx.StaticText(self,-1,dic['config graph color'])
box.Add(label,1,wx.ALIGN_CENTRE|wx.ALL, 5)
self.graph=wx.Choice(self,-1,(100, 50),choices = ALL_COLORS)
self.graph.Select(ALL_COLORS.index(color['graph']))
box.Add(self.graph,0,wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
box=wx.BoxSizer(wx.HORIZONTAL)
label=wx.StaticText(self,-1,dic['config skale color'])
box.Add(label,1,wx.ALIGN_CENTRE|wx.ALL, 5)
self.skale=wx.Choice(self,-1,(100, 50),choices = ALL_COLORS)
self.skale.Select(ALL_COLORS.index(color['skale']))
box.Add(self.skale,0,wx.ALIGN_CENTRE|wx.ALL, 5)
sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)
btnsizer = wx.StdDialogButtonSizer()
btn = wx.Button(self, wx.ID_OK)
btn.SetDefault()
btnsizer.AddButton(btn)
btn = wx.Button(self, wx.ID_CANCEL)
btnsizer.AddButton(btn)
btnsizer.Realize()
sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
self.SetSizer(sizer)
sizer.Fit(self)
class draw_window(wx.Window):
def __init__(self,parent,id,dic, pos=wx.DefaultPosition, size=wx.DefaultSize):
self.color=COLOR
self.dic=dic
self.x=20
self.y=20
self.function=''
wx.Window.__init__(self, parent, id, pos, size)
self.SetBackgroundColour(wx.NamedColour(self.color['bg']))
if wx.Platform == '__WXGTK__':
self.font = wx.Font(12, wx.MODERN, wx.NORMAL, wx.NORMAL)
else:
self.font = wx.Font(10, wx.MODERN, wx.NORMAL, wx.NORMAL)
self.SetFocus()
self.Bind(wx.EVT_PAINT, self.on_paint)
self.Bind(wx.EVT_SIZE, self.on_size)
def draw(self,function=None,dc=None):
if not function is None:
self.function=function
else:
function=self.function
if dc is None:
dc = wx.ClientDC(self)
dc.SetFont(self.font)
dc.Clear()
(x_max,y_max) = self.GetSizeTuple()
middle_x=x_max/2
middle_y=y_max/2
dc.DrawText('y='+function,middle_x+10,10)
dc.DrawText('%0.3f auf x == 1 Pixel'%(1.0/self.x),middle_x+15,21)
dc.DrawText('%0.3f auf y == 1 Pixel'%(1.0/self.y),middle_x+15,32)
dc.SetPen(wx.Pen(wx.NamedColour(self.color['skale']), 2))
dc.DrawLine(middle_x,0,middle_x,y_max)
dc.DrawLine(0,middle_y,x_max,middle_y)
for i in range(1,middle_x,self.x):
dc.DrawLine(middle_x+i,middle_y+1,middle_x+i,middle_y-2)
for i in range(1,middle_x,self.x):
dc.DrawLine(middle_x-i,middle_y+1,middle_x-i,middle_y-2)
for i in range(1,middle_y,self.y):
dc.DrawLine(middle_x+1,middle_y-i,middle_x-2,middle_y-i)
for i in range(1,middle_y,self.y):
dc.DrawLine(middle_x+1,middle_y+i,middle_x-2,middle_y+i)
dc.SetPen(wx.Pen(wx.NamedColour(self.color['graph']), 1))
x=(-middle_x-1)/float(self.x)
old=int(self.y*eval(function))
for i in range(-middle_x,middle_x,1):
x=i/float(self.x)
new=int(self.y*eval(function))
dc.DrawLine(i+middle_x-1,-old+middle_y,i+middle_x,-new+middle_y)
old=new
def config(self,x_mult,y_mult,bg,graph,skale):
self.x=int(x_mult)
self.y=int(y_mult)
self.SetBackgroundColour(wx.NamedColour(ALL_COLORS[bg]))
self.color['bg']=ALL_COLORS[bg]
self.color['skale']=ALL_COLORS[skale]
self.color['graph']=ALL_COLORS[graph]
def on_paint(self, event):
dc = wx.PaintDC(self)
self.draw(self.function,dc)
def on_size(self,event):
self.Refresh()
class myframe(wx.Frame):
def __init__(self,parent,dic):
self.dic=dic
wx.Frame.__init__(self,parent,-1,dic['title'],wx.DefaultPosition,(400,300))
self.wnd=draw_window(self,-1,dic)
menu=wx.Menu()
menu.Append(1001,dic['new'],dic['new status bar'])
menu.AppendSeparator()
menu.Append(1002,dic['exit'],dic['exit status bar'])
menubar=wx.MenuBar()
menubar.Append(menu,dic['file'])
menu=wx.Menu()
menu.Append(1003,dic['config'],dic['config status bar'])
menubar.Append(menu,dic['edit'])
self.SetMenuBar(menubar)
self.CreateStatusBar(2)
self.Bind(wx.EVT_MENU, self.on_new, id=1001)
self.Bind(wx.EVT_MENU, self.on_end, id=1002)
self.Bind(wx.EVT_MENU, self.on_config,id=1003)
self.Bind(wx.EVT_CLOSE, self.on_end)
self.on_new(None)
def on_new(self,event):
self.wnd.draw('sin(x)')
def on_end(self,event):
dlg = wx.MessageDialog(self, self.dic['quest exit'],
self.dic['exit title'], wx.YES_NO | wx.ICON_QUESTION)
ret = dlg.ShowModal()
dlg.Destroy()
if ret == wx.ID_YES:
self.Destroy()
else:
event.Skip()
def on_config(self,event):
dlg=config_dlg(None,-1,self.dic,self.wnd.x,self.wnd.y,self.wnd.color)
dlg.CenterOnScreen()
dlg.ShowModal()
dlg.bg.GetSelection()
try:
self.wnd.config(float(dlg.x.GetValue())
,float(dlg.y.GetValue())
,dlg.bg.GetSelection()
,dlg.graph.GetSelection()
,dlg.skale.GetSelection())
except ValueError:
pass
else:
self.wnd.draw()
dlg.Destroy()
class app(wx.App):
def OnInit(self):
frame = myframe(None,DEUTSCH)
self.SetTopWindow(frame)
frame.Show(True)
return True
if __name__=='__main__':
main=app()
main.MainLoop()