2017-08-03 48 views
0

我有2類:傳遞參數值內/外的類

  1. schedule_GUI主管創建和填充缺省值(從csv文件讀取)。
  2. run_schedule負責根據時間表開啓/關閉物理交換機。

schedule_GUI是主爲run_schedule類共享csv文件數據(除其他外),在__init__

class run_schedule(): 

    def __init__(self,master): 
     self.master = master 
     self.sch_file = master.sch_file #### sch.file is retrieved from master 
     self.continue_task = master.continue_task 
     self.tasks_total = master.tasks_total 
     self.loop() 

正如你所看到的,在下面的代碼我可以有多個計劃在不同的標籤

NOW-時間表可以使用圖形用戶界面進行更新和保存csv文件(的schedule_GUI一部分),但是當它發生它不更新sch__filerun_schedule

問題:主人如何更新自己的孩子?

init_parameters() 

##Initialize GUI 
root = Tk() 
root.title("Advanced Schedualer") 

note = tkinter.ttk.Notebook(root) 
tab1 = tkinter.ttk.Frame(note)#,height=40) 
tab2 = tkinter.ttk.Frame(note) 
note.add(tab1, text = "Tab One", compound=TOP) 
note.add(tab2, text = "Tab Two") 
note.grid() 

#Create GUI in [email protected] 
gui1 = schedule_GUI(tab1, num_switches) 
gui2 = schedule_GUI(tab2, num_switches) 

#Run Loop Shedules 
loop1 = run_schedule(gui1) 
loop2 = run_schedule(gui2) 

root.mainloop() 
+0

由於'run_schedule'類具有對存儲在'schedule_GUI'實例的引用在'self.master'中,它可以輪詢它的變化(我在'self.loop()'中猜測)。不幸的是,在你的問題中沒有足夠的代碼來向你展示如何做到這一點。另外,您應該閱讀並開始遵循[PEP 8 - Python代碼風格指南](https://www.python.org/dev/peps/pep-0008/),特別是關於變量和類的命名,如以及推薦的縮進代碼的方式。 – martineau

+0

你能解釋更多關於投票嗎? –

+1

輪詢只是重複檢查的東西。在這種情況下,主類需要有一個子類實例可以定期調用的方法來查看是否需要更新'sch__file'。您也可以安排'tkinter'在經過一定時間後調用函數(並重復執行該操作以保持重複)。可以使用名爲['after']的通用窗口小部件方法(http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html)來設置此類事物。 – martineau

回答

0

爲了讓我寫此代碼demonstate不同類之間傳遞的值(內和外)的示例:

class A: 
    class C: 
    def __init__(self, master, v, i): 
     self.x = v + i 
     print("C variable was created") 
     print("in C Class, and my master is:", master.firstname) 
     self.CB = B(master) # create B Class var and passing A Class as Master 

    def print_c(self): 
     print("running function print_c inside Class C", self.x) 

def __init__(self, first, last, birthday, *args): 
    print("A class variable was created") 
    self.firstname = first 
    self.lastname = last 
    self.bday = birthday 
    #self.email() 
    self.c1 = self.C(self, *args) #Create C Class variable 

def email(self): 
    def doit(arg1): 
     print("this is value inside", arg1) 

    print(self.firstname + '.' + self.lastname + '@' + 'gmail.com') 
    doit(self.bday) 
    print("this is value outside", self.bday) 

class B: 
def __init__(self, master): 
    print("B Class created, inheret A Class parameters via a C Class:", master.firstname, master.lastname) 



#running script : 

a1 = A("Jhon", 'Doe', '13/12/1985', 11, 12) # create a1- an A Class variable with parameters 
print("My email value from A Class:", a1.email()) # calling methon "email" belongs to a1 
a1.c1.print_c() # calling c1 variable, part of a1 variable declared earlier