python 3.x - Need to call class method from different class without initialization of the first class or some other way around it -
i have small problem code.
there 2 classes. first 1 creates window options button. upon clicking button, second class called , creates window ok button. let's there checkbox, changes background color black or that. after clicking button, whatever changes made in options stored file , second window closed.
all of works fine. problem need call method update_init first class apply changes mainwindow. code below shows first solution problem, understand, using second mainloop create second thread, should avoided.
class mainwindow: def __init__(self, master): self.master = master self.options_btn = tk.button(self.master, text="options", command=self.open_options) self.options_btn.pack() self.options_window = none def open_options(self): options_master = tk.toplevel() self.options_window = optionswindow(options_master) options_master.mainloop() lst = meta_load() # loads changes file self.update_init(lst) def update_init(self, lst): #code class optionswindow: def __init__(self, master): self.master = master self.ok_btn = tk.button(self.master, text="ok", command=self.update_meta) self.ok_btn.pack() def update_meta(self): meta_save(12) # saves changes file self.master.destroy() main_master = tk.tk() main_master.minsize(width=1280, height=720) b = mainwindow(main_master) main_master.mainloop()
my second solution put both classes one, code quite messy if so. can somehow call method update_init (which in mainwindow class) optionswindow class without initializing new mainwindow class window? or there other way deal this? appreciate help.
i sorry if specific, i've tried make general possible, it's specific problem , couldn't find information anywhere on internet.
in general can call class method anywhere want , pass without initialisation of class's instance, objective nature of python, beware of self
dependencies! although, don't think that's practice.
class a: def __init__(self): self.foo = 'foo' def return_foo(self): return self.foo class b: def __init__(self): self.bar = 'bar' print('ha-ha im inited!') def return_bar(self): try: return self.bar except attributeerror: return 'bar' def test(): = a() # b = b() return_bar = getattr(b, 'return_bar', none) if callable(return_bar): print('%s%s' % (a.return_foo(), return_bar(none))) test()
links:
Comments
Post a Comment