异常结束时如何返回尝试脚本

时间:2019-02-18 19:59:50

标签: python-3.x tkinter try-catch digital-compass

最近我正在尝试编写GUI脚本,该脚本将捕获来自QMC5883l数字罗盘的数据并实时在小gui中显示数据,该脚本可以正常工作,并且没有错误,除非指南针断开连接,OSError正常上升,并且程序停止,我用try-catch来解决这个问题,所以现在当异常引发它很好地表明指南针断开连接时,但是当指南针重新连接时它将不再像存储在oserror异常代码中一样工作 我需要知道错误结束后是否有任何方法可以返回尝试脚本

我的代码在下面

# This Python file uses the following encoding: utf-8
import smbus            #import SMBus module of I2C
from time import sleep  #import sleep
import math
import sys
import os
from tkinter import *
#some MPU6050 Registers and their Address                         
Register_mode  = 0x09           #Address of mode register
Register_set_reset = 0x0b       #Addreas of set/reset period
X_axis_H    = 0x01              #Address of X-axis MSB data register
Z_axis_H    = 0x03              #Address of Z-axis MSB data register
Y_axis_H    = 0x05              #Address of Y-axis MSB data register
declination = 1.466          #define declination angle of location where measurement going to be done
pi          = 3.14159265359     #define pi value

def Magnetometer_Init():
        #Write to mode R`egister for selecting mode
        bus.write_byte_data(Device_Address, Register_mode, 0x1d)
        bus.write_byte_data(Device_Address, Register_set_reset , 0x01)
def read_raw_data(addr):
          #Read raw 16-bit value
        high = bus.read_byte_data(Device_Address, addr)
        low = bus.read_byte_data(Device_Address, addr-1)
        #concatenate higher and lower value
        value = ((high << 8) | low)

        #to get signed value from module
        if(value > 32768):
            value = value - 65536
        return value


bus = smbus.SMBus(1)    # or bus = smbus.SMBus(0) for older version boards
Device_Address = 0x0d   #QMC5883L magnetometer device address

Magnetometer_Init()     # initialize qMC5883L magnetometer 
print (" Reading Heading Angle")
window = Tk()
window.title("Welcome to LikeGeeks app")
window.geometry('350x200')
lbl = Label(window, text='heading angle')
lbl.grid(column=0, row=0)

def mainloop1():
        try:
        #Read Accelerometer raw value
                x = read_raw_data(X_axis_H)
                z = read_raw_data(Z_axis_H)
                y = read_raw_data(Y_axis_H)
        #Due to declination check for >360 degree
                heading = math.atan2(y, x) + declination        
                if(heading > 2*pi):
                        heading = heading - 2*pi

                #check for sign
                if(heading < 0):
                        heading = heading + 2*pi

                #convert into angle
                heading_angle = int(heading * 180/pi)
                lbl.configure(text=heading_angle)
                window.after(1,mainloop1)
          except OSError:
                  lbl.configure(text='compass disconnected')
window.after(0, mainloop1)      
window.mainloop()

1 个答案:

答案 0 :(得分:0)

由于window.after(1, mainloop1)try块内被调用,一旦出现异常,它将不会被执行并且mainloop1()也不会再次执行。

要克服它,只需将window.after(1, mainloop1)try块移到mainloop1()的末尾,如下所示:

def mainloop1():
    try:
        ...
        lbl.configure(text=heading_angle)
    except OSError as err:
        lbl.configure(text='compass disconnected')
    window.after(1, mainloop1)

然后window.after(1, mainloop1)将被执行,即使发生异常。