Kann man eine Funktion auch so schreiben, dass sie generell genug ist, um sich auf beide Knoepfe zu beziehen, aber dann fuer die zwei verschiedenen TextCtrls jeweils spezifisch einen passenden return Value zurueckgibt?
Der Grund, warum ich frage ist, weil ich in dem eigentlichen Code nicht 2 sonder 15 verschiedene Zeilen habe. Ich war immer der Ansicht, dass (15 mal) copy and paste von code nicht besonders "pythonic" ist. Nur weiss ich mir mal wieder nicht zu helfen.
Danke euch schonmal fuer Feedback und Kritik
NOTE: Bitte beachten, dass dies Beispielcode ist, der auf das Essentielle herunter getrimmt wurde.
Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-15 -*-
# generated by wxGlade 0.6.3 on Sun May 30 21:43:52 2010
import wx
import os
# begin wxGlade: extracode
# end wxGlade
ID_ONE = wx.NewId()
ID_TWO = wx.NewId()
class MyDialog(wx.Dialog):
def __init__(self, *args, **kwds):
# begin wxGlade: MyDialog.__init__
kwds["style"] = wx.DEFAULT_DIALOG_STYLE
wx.Dialog.__init__(self, *args, **kwds)
self.PathOneLabel = wx.StaticText(self, -1, "Path 1")
self.PathOneText = wx.TextCtrl(self, -1, "")
self.PathOneBtn = wx.Button(self, ID_ONE, " ... ")
self.PathTwoLabel = wx.StaticText(self, -1, "Path 2")
self.PathTwoText = wx.TextCtrl(self, -1, "")
self.PathTwoBtn = wx.Button(self, ID_TWO, " ... ")
##############################################
#### Button Bindings #########################
##############################################
wx.EVT_BUTTON(self, ID_ONE, self.onPathOne)
wx.EVT_BUTTON(self, ID_TWO, self.onPathTwo)
self.__set_properties()
self.__do_layout()
# end wxGlade
def __set_properties(self):
# begin wxGlade: MyDialog.__set_properties
self.SetTitle("Example")
# end wxGlade
def __do_layout(self):
# begin wxGlade: MyDialog.__do_layout
grid_sizer_1 = wx.GridSizer(4, 2, 0, 0)
grid_sizer_1.Add(self.PathOneLabel, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add(self.PathOneText, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add(self.PathOneBtn, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add(self.PathTwoLabel, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add((20, 20), 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add(self.PathTwoText, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
grid_sizer_1.Add(self.PathTwoBtn, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 0)
self.SetSizer(grid_sizer_1)
grid_sizer_1.Fit(self)
self.Layout()
# end wxGlade
#####################################################################
########## The code in question #####################################
#####################################################################
def onSetPath(self):
"Sends a Path Config Dialog"
# Create a file-open dialog in the current directory
dlg = wx.FileDialog(self, message="Open a file", defaultDir=os.getcwd(),
defaultFile="", style=wx.OPEN)
# Call the dialog as a model-dialog so we're required to choose Ok or Cancel
if dlg.ShowModal() == wx.ID_OK:
# User has selected something, get the path, set the window's title to the path
#filename = dlg.GetPath()
#self.townEurText.SetValue(filename)
return dlg.GetPath()
dlg.Destroy()
# Geht das hier auch kuerzer? Bei dem wirklich code waeren das 15 verschiedene Funktionen,
# die immer nur leichte Aenderungen im Variablennamen haetten
def onPathOne(self, event):
filename = self.onSetPath()
self.PathOneText.SetValue(filename)
def onPathTwo(self, event):
filename = self.onSetPath()
self.PathTwoText.SetValue(filename)
#################################################################
################# End of relevant code ##########################
#################################################################
# end of class MyDialog
if __name__ == "__main__":
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
Example = MyDialog(None, -1, "")
app.SetTopWindow(Example)
Example.Show()
app.MainLoop()