1
因此,我試圖創建一個try/except塊來運行此代碼,他們應該能夠在彈出窗口中單擊它們以打印出答案,並且然後按計算按鈕重新打開它。然而,當我去按計算按鈕第二次,它給了我Kivy TypeError - 標籤對象在Try/Except子句中不可調用
TypeError: 'Label' object is not callable
我把下面的代碼:
class TabSys(TabbedPanel):
savedfiles_hydraulus = []
def calculate_psc_clicked(self, Cp_text, P_text, lhv_text):
global psychrometric_constant
#when they click the button which is id'd as "calculate_psc," this function
#will pull the values and perform the calculations
try:
psychrometric_constant = (float(Cp_text)*float(P_text))/(float(lhv_text)*float(2.26))
self.psc_answer()
except ValueError:
"""Needs a popup for wrong results"""
#the popups print out the answers and offer the user an option to save or dismiss the window
def psc_answer(self):
global Flag
Flag = False
#Layout
popupscreen = FloatLayout()
self.psc_notes_label = Label(text = "Psychrometric constant: (Cp*P)/(λ*MWr)", pos_hint = {"center_x": 0.5, "center_y": 0.7})
self.psc_answer = Label(text = str(psychrometric_constant), pos_hint = {"center_x": 0.5, "center_y": 0.4})
popupscreen.add_widget(self.psc_notes_label)
popupscreen.add_widget(self.psc_answer)
#Dismiss Window
self.cancel_psc_answer = Button(text = "Cancel", pos_hint = {"center_x": .575, "center_y": .065}, size_hint = (.3, .2))
self.cancel_psc_answer.bind(on_release = self.dismisspsc)
popupscreen.add_widget(self.cancel_psc_answer)
#Window Setup
self.popup = Popup(title="Result", content = popupscreen, size_hint=(.5, .5), size=(400, 400),
separator_color = [217/255, 179/255, 255/255., .85])
self.popup.open()
#Dismiss Function
def dismisspsc(self, *args):
self.popup.dismiss()
它是相當多的,不幸的是,但我儘可能多地剪掉。如果有人需要,我可以將pastebin與全部代碼鏈接起來,因爲我現在擁有它。
我認爲這個問題可能是該行
try:
psychrometric_constant = (float(CP_text)*float(P_text))/(float(lhv_text)*float(2.26))
是在try/except塊,它可以更好地psc_answer的函數內即成。
非常感謝:)
這裏是我的代碼的全部:https://pastebin.com/p2eCdPRX –