tkinter - Is it bad to use instance methods to organize code in a large python class -
below have dummie code, instance methods definied code organization. init function sets chain of calls various instance methods in specific order, , starting feel bad practice have code organized in such way.
if there references on organizing large classes (such found in gui apps) improve readability?
dummie code:
class app: def __init__(self): def do_things() def do_things(self): self.do_a_thing() self.do_another_thing() def do_a_thing(self): self.thing1 = 1 def do_another_thing(self): self.thing2 = self.thing1 + 1
these functions execute once when class called, suggest should not functions (or instance methods)?
a working example have chosen potentially bad method of organization
below, fill()
method akin do_things()
, , e.g. init_scroll()
, init_lb()
akin do_a_thing()
, do_another_thing()
, etc.
import tkinter tk import tkfont class editorapp: def __init__( self, master) : self.root = master self.root.title('editor') self.main = tk.frame( self.root ) self.main.pack(fill=tk.both, expand=true) self.fill() ################## # adding widgets # ################## def fill( self): self.canvas = tk.canvas( self.main ) self.canvas.pack(fill=tk.both, expand=tk.yes) self.init_scroll() self.init_lb() self.pack_config_scroll() self.pack_bind_lb() self.fill_listbox() ############## # scrollbars # ############## def init_scroll(self): self.scrollbar = tk.scrollbar(self.canvas, orient="vertical") self.xscrollbar = tk.scrollbar(self.canvas, orient="horizontal") def pack_config_scroll(self): self.scrollbar.config(command=self.lb.yview) self.xscrollbar.config(command=self.xview) self.scrollbar.pack(side="right", fill="y") self.xscrollbar.pack(side="bottom", fill="x") def onmousewheel(self, event): self.lb.yview("scroll", event.delta,"units") return "break" def xview(self, *args): self.lb.xview(*args) ################ # main listbox # ################ def init_lb( self): self.lb = tk.listbox(self.canvas, font=tkfont.font(self.canvas, family="courier", size=14), yscrollcommand=self.scrollbar.set, xscrollcommand=self.xscrollbar.set, exportselection=false) def pack_bind_lb(self): self.lb.pack(fill="both", expand=true) self.lb.bind("<mousewheel>", self.onmousewheel) def fill_listbox(self): dummie_lines = [ 'line%d\t'%x+ ''.join(['allworkandnoplaymakesjackadullboy']*100) x in xrange(50) ] line in dummie_lines: self.lb.insert(tk.end, line) if __name__ == '__main__': root = tk.tk() editor = editorapp(root) root.mainloop()
i have attempted group similar methods sake of debugging (the code pulled class 300 lines), not know lot accepted style guidelines.
also, full application posted here
is concern developers may call do_thing
independent of do_things
? while python doesn't support private methods, can indicate do_thing
should treated private using leading underscore convention:
def _do_thing(self): ...
this tells developers not call _do_thing
directly.
generally, it's idea break large methods or functions smaller, more manageable ones. can important when unittesting code.
Comments
Post a Comment