亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何處理意外按鍵

如何處理意外按鍵

jeck貓 2024-01-15 21:43:09
我設計了一個有兩個按鈕的應用程序,即CAL和SAV。因此,我有兩個功能,但問題是有時生產線操作員會錯誤地按下SAV按鈕。這樣就會出現屬性錯誤并導致程序卡住。如何克服這個問題?請指導我。這是我的代碼:class ADS1x15:    """Base functionality for ADS1x15 analog to digital converters."""class ADS1115(ADS1x15):    """Class for the ADS1115 16 bit ADC."""class AnalogIn:    """AnalogIn Mock Implementation for ADC Reads."""import RPi.GPIO as GPIOimport tkinter as tkGPIO.setmode(GPIO.BCM)GPIO.setup(12,GPIO.IN)  #Save ButtonGPIO.setup(5,GPIO.IN)  #Cal  Buttonroot=tk.Tk()root.geometry("1000x600")file = open("/home/pi/data_log.txt", "r")   f = file.read().split(',')   rangeh = int(f[3])   offset = int(f[4])   fullScale = int(f[5])chan=AnalogIn(ads,P0,P1)def cal(channel):       global Dsel,cal_c,rangeh,offset,fullScale,chan       cal_c = cal_c + 1       if cal_c == 1:          root.L1 = tk.Label(root,text="Put Zero Weight and Press CAL btn",fg="brown",font="bold")          root.L1.pack()          root.L1.place(x=1,y=1)       elif cal_c == 2:          root.L1.destroy()          offset = chan.value          file = open("/home/pi/data_log.txt", "w")          if os.stat("/home/pi/data_log.txt").st_size == 0:             file.write("rangeh,offset,Full_Scale,\n")          file.write(str(rangeh)+","+str(offset)+","+str(fullScale))          file.flush()          root.L2 = tk.Label(root,text="Put Full Weight and Press SAV btn",fg="brown",font="bold")          root.L2.pack()          root.L2.place(x=1,y=1)          def sav(channel):       global rangeh,offset,fullScale       file = open("/home/pi/data_log.txt", "w")       if os.stat("/home/pi/data_log.txt").st_size == 0:          file.write("rangeh,offset,Full_Scale,\n")       file.write(str(rangeh)+","+str(offset)+","+str(fullScale))       file.flush()               root.L2.destroy()此錯誤是由于root.L2.destroy()這一行而產生的。我可以阻止或禁用此sav函數,以便在不調用cal函數的情況下它不會執行嗎?
查看完整描述

1 回答

?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

強力解決方案是檢查 root 是否具有 L2 屬性


from tkinter import messagebox    

def sav(channel):

    if hasattr(root, 'L2'):

        global rangeh, offset, fullScale

        file = open("/home/pi/data_log.txt", "w")

        if os.stat("/home/pi/data_log.txt").st_size == 0:

            file.write("rangeh,offset,Full_Scale,\n")

        file.write(str(rangeh) + "," + str(offset) + "," + str(fullScale))

        file.flush()


        root.L2.destroy()

    else:

        messagebox.showinfo('Unable to save', 'No data was generated yet')

更優雅的方法是在啟動時禁用保存按鈕,僅在執行 cal 函數后才啟用它。


我對 Raspberry Pi 的實現不太熟悉,因此這只是如何實現按鈕禁用的粗略草圖:從外觀上看,按鈕是通過 GPIO.add_event_detect 函數“連接”的。


所以我會從主腳本中刪除 sav-callback 并在 cal 腳本之后動態添加它,如下所示:


# [...] beginning of your script [...]

def cal(channel):

    # [...] original body of cal function [...]

    activate_save_button()


def activate_save_button():

    GPIO.add_event_detect(12, GPIO.RISING, callback=sav, bouncetime=1000)

    

def deactivate_save_button():

    GPIO.remove_event_detect(12)


def sav(channel):

    # [...] original body of sav function [...]

    # remove save button functionality after saving

    deactivate_save_button()


def update():

    """ function for continuous show value in every 500ms in tkinter window"""



GPIO.add_event_detect(5, GPIO.RISING, callback=cal, bouncetime=1000)

# line with callback=sav is deleted here


root.after(500, update)

root.mainloop()


查看完整回答
反對 回復 2024-01-15
  • 1 回答
  • 0 關注
  • 143 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號