2016-05-30 54 views
1

我自己學習Python和Tkinter,並且對於我正在編寫的這段代碼有一些疑問。 我想讓程序執行兩個屬於類的功能。 因此,當我嘗試執行下面的代碼tkinter執行第一個函數,但不執行功能2,我不明白爲什麼。 有人可以幫助我這個。 而在函數2中,j的值需要等於來自函數1的j的值。 謝謝。Python Tkinter - 如何使用從一個函數到另一個函數的值?

from tkinter import * 
myapp=Tk() 
myapp.geometry('1100x700+100+50') 
myapp.title('List Generator') 

input1=IntVar() 
input2=IntVar() 

class Myclass: 
    def __init__(self): 
     text1=input1.get() 
     text2=input2.get() 
     ex1=float(text1) 
     ex2=float(text2) 
     totbtu=float(ex1*ex2) 
     realbtu=totbtu+(totbtu*0.15) 
     j= float(totbtu + 100) 
     Label(myapp, text=totbtu).place(x=600,y=20) 
     Label(myapp, text=realbtu).place(x=600,y=60) 
     Label(myapp, text=j).place(x=600,y=100) 

    def function2(self): 
     h=j+33 
     Label(myapp, text=h).place(x=600,y=140) 

label1 = Label(myapp, text='Enter Area').place(x=10,y=10) 
area_entry=Entry(myapp,textvariable=input1).place(x=140,y=10) 
label11 = Label(myapp,text='SQ FT',).place(x=270,y=10) 

label2 = Label(myapp, text='Enter Load').place(x=10,y=35) 
area_entry=Entry(myapp,textvariable=input2).place(x=140,y=35) 
label22 = Label(myapp,text="BTU's/SQ FT",).place(x=270,y=35) 

button1 = Button(myapp, text = 'Generate',padx=5,pady=5,command=Myclass).place(x=10,y=70) 

myapp.mainloop() 

回答

0

如果我正確理解你的問題,你需要調用一個按鈕命令2個功能和你還需要在這兩個函數(我假設)使用一個值。

看看我的解決方案:

from Tkinter import * 
myapp=Tk() 
myapp.geometry('1100x700+100+50') 
myapp.title('List Generator') 

input1=IntVar() 
input2=IntVar() 
j = 0 #needed for the value to be referenced in both function1 and function2 
def function1(): 
    global j 
    text1=input1.get() 
    text2=input2.get() 
    ex1=float(text1) 
    ex2=float(text2) 
    totbtu=float(ex1*ex2) 
    realbtu=totbtu+(totbtu*0.15) 
    j= float(totbtu + 100) 
    Label(myapp, text=totbtu).place(x=600,y=20) 
    Label(myapp, text=realbtu).place(x=600,y=60) 
    Label(myapp, text=j).place(x=600,y=100) 
    print(j) 

def function2(): 
    global j 
    h=j+33 
    Label(myapp, text=h).place(x=600,y=140) 
    print(j) 

def wombocombo(): 
    function1() 
    function2() 
label1 = Label(myapp, text='Enter Area').place(x=10,y=10) 
area_entry=Entry(myapp,textvariable=input1).place(x=140,y=10) 
label11 = Label(myapp,text='SQ FT',).place(x=270,y=10) 
label2 = Label(myapp, text='Enter Load').place(x=10,y=35) 
area_entry=Entry(myapp,textvariable=input2).place(x=140,y=35) 
label22 = Label(myapp,text="BTU's/SQ FT",).place(x=270,y=35) 

button1 = Button(myapp, text = 'Generate',padx=5,pady=5,command=wombocombo).place(x=10,y=70) 

myapp.mainloop() 

當您口述功能時按下按鈕來調用,您可以使用command=myFunction myFunction是一個函數,而不是一類。就像這樣,當像Myclass這樣的類是命令而不是函數時,就會出現問題。

要使用1個命令執行2個功能,只是兩者的功能合併爲1。如果你看看在wombocombo功能,它會調用都function1function2,擊中二鳥1石。


至於分享兩種功能之間的變量,我總是發現最簡單去的範圍更廣泛的梯子一步,並添加我的變量存在。

在本例中,我們在function1function2中都需要相同的j變量。範圍越來越廣泛,我們在一個function1function2可以訪問的地方定義j。

+0

非常感謝您的幫助槍手。我剛開始學習使用python編寫代碼,所以這非常有幫助。 – PeterS

+0

嗨槍手,我仍然有這個代碼的問題,因爲在function2我想使用function1的j值。 – PeterS

+0

@PeterS嗨,謝謝你讓我知道。我不知道我以前在想什麼,但j在兩個函數中應該是相同的。我在每個函數的第一行添加了兩行,一行告訴程序「在這個函數中,使用全局變量j,不使用本地」。我所做的編輯應該有所幫助!再次感謝。 –

相關問題