Почему имя моей переменной определено в root, но не распознается на верхнем уровне? - PullRequest
0 голосов
/ 05 августа 2020

Я работаю над программой, которая будет выполнять вычисления, необходимые для проверки калибровки лабораторных пипеток. В первом (root) GUI окне пользователь вводит информацию. Часть root - это функция (выбранная), которая позволяет пользователю выбрать одну из трех различных моделей дозатора из раскрывающегося списка. Выбор работает нормально в root, но когда я пытаюсь вызвать его из Toplevel, я получаю следующую ошибку:

 Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/Users/seanchickery/PycharmProjects/Giraffe/pipette.py", line 102, in analyze
    Absolute_Error_1 = float(Volume1.get()) - Vol1_Mean
AttributeError: 'Label' object has no attribute 'get'

Я пробовал различные попытки решить, но не могу понять. Это моя первая программа в Python. Я разместил весь свой код ниже.

root = Tk()
root.geometry("1000x1600")
root.title("Pipette Calibration Verification")
Title = Label(root, text="PIPETTE CALIBRATION VERIFICATION", bg="grey", width="800", justify=CENTER)
Title.pack()
url = "https://drive.google.com/file/d/1XYLJrb-MB10SfxM_H0XSKtEBGMflNm8x/view?usp=sharing"

#Pipette Selection Function
def selected(event):
    global Volume1
    global Volume2
    global Volume3
    if Clicked.get() == "Blue 50-1,000 µl":
        Volume1 = Label(root, text="1.0000")
        Volume2 = Label(root, text="0.5000")
        Volume3 = Label(root, text="0.1000")
        Volume1.place(x=175, y=520)
        Volume2.place(x=525, y=520)
        Volume3.place(x=825, y=520)
    elif Clicked.get() == "Yellow 5-100 µl":
        Volume1 = Label(root, text="0.1000")
        Volume2 = Label(root, text="0.0500")
        Volume3 = Label(root, text="0.0100")
        Volume1.place(x=175, y=520)
        Volume2.place(x=525, y=520)
        Volume3.place(x=825, y=520)
    elif Clicked.get() == "Purple 0.2 – 5 ml":
        Volume1 = Label(root, text="5.0000")
        Volume2 = Label(root, text="2.5000")
        Volume3 = Label(root, text="0.500")
        Volume1.place(x=175, y=520)
        Volume2.place(x=525, y=520)
        Volume3.place(x=825, y=520)
    else:
        Volume1 = Entry(root)
        Volume2 = Entry(root)
        Volume3 = Entry(root)
        Volume1.place(x=175, y=520, width=75)
        Volume2.place(x=525, y=520, width=75)
        Volume3.place(x=825, y=520, width=75)

def convert():
    Barometric_Int = float(Barometric_Pressure.get())
    Barometric_Convert = Barometric_Int * 33.863886666667
    Barometric_Convert_as_String = str(Barometric_Convert)
    Barometric_Conversion = "Barometric pressure in hPa: " + Barometric_Convert_as_String
    Barometric_Display = Label(root)
    Barometric_Display["text"] = Barometric_Conversion
    Barometric_Display.place(x=510, y=165)

# Functions - Open results page
def analyze():
    global Volume1
    global Volume2
    global Volume3
    Top = Toplevel()
    Top.geometry("1000x1600")
    Top.title("Pipette Calibration Verification Results")
    Title = Label(Top, text="PIPETTE CALIBRATION VERIFICATION", bg="grey", width="800", justify=CENTER)
    Date_Get = Date_Entry.get()
    serial_get = Serial_Entry.get()
    Pipette_SN = Label(Top, text="Pipette Serial Number: " + serial_get)
    Name_Get = Name_Entry.get()
    Name_Form = Label(Top, text="Individual Performing Verification: " + Name_Get)
    Date_Form = Label(Top, text="Date Test Performed: " + Date_Get)
    #Volume 1 math and display
    Correction_Factor_Int = float(Correction_Factor.get())
    Weight1_1_Int = float(Weight1_1.get())
    CWeight1_1 = Correction_Factor_Int * Weight1_1_Int
    Weight1_2_Int = float(Weight1_2.get())
    CWeight1_2 = Correction_Factor_Int * Weight1_2_Int
    Weight1_3_Int = float(Weight1_3.get())
    CWeight1_3 = Correction_Factor_Int * Weight1_3_Int
    Weight1_4_Int = float(Weight1_4.get())
    CWeight1_4 = Correction_Factor_Int * Weight1_4_Int
    Weight1_5_Int = float(Weight1_5.get())
    CWeight1_5 = Correction_Factor_Int * Weight1_5_Int
    Weight1_6_Int = float(Weight1_6.get())
    CWeight1_6 = Correction_Factor_Int * Weight1_6_Int
    Weight1_7_Int = float(Weight1_7.get())
    CWeight1_7 = Correction_Factor_Int * Weight1_7_Int
    Weight1_8_Int = float(Weight1_8.get())
    CWeight1_8 = Correction_Factor_Int * Weight1_8_Int
    Weight1_9_Int = float(Weight1_9.get())
    CWeight1_9 = Correction_Factor_Int * Weight1_9_Int
    Weight1_10_Int = float(Weight1_10.get())
    CWeight1_10 = Correction_Factor_Int * Weight1_10_Int
    Vol1_Mean = (CWeight1_1 + CWeight1_2 + CWeight1_3 + CWeight1_4 + CWeight1_5 + CWeight1_6 + CWeight1_7 + CWeight1_8 + CWeight1_9 + CWeight1_10)/10
    Vol1_Rd = round(Vol1_Mean, 4)
    Vol1_String = str(Vol1_Rd)
    Volume1_Final = "The corrected arithmetic mean volume for the max volume is " + Vol1_String + "mL."
    Volume1_Form = Label(Top)
    Volume1_Form["text"] = Volume1_Final
    Absolute_Error_1 = float(Volume1.get()) - Vol1_Mean #***HERE IS THE PROBLEM LINE***
    Absolute1_Abs = abs(Absolute_Error_1)
    Absolute1_Rd = round(Absolute1_Abs, 3)
    Absolute1_Final = "Absolute Systematic error: " + str(Absolute1_Rd) + "mL."
    Absolute1_Form = Label(Top)
    Absolute1_Form["text"] = Absolute1_Final
    Relative1_Initial = (Absolute1_Rd * 100) / float(Volume1.get())
    Relative1_Rd = round(Relative1_Initial, 1)
    Relative1_Final = "Relative Systematic Error: " + str(Relative1_Rd) + "mL."
    Relative1_Form = Label(Top)
    Relative1_Form["text"] = Relative1_Final
    AREM1_Math = math.sqrt(
        ((abs(CWeight1_1 - Vol1_Mean) ** 2 + abs(CWeight1_2 - Vol1_Mean) ** 2 + abs(CWeight1_3 - Vol1_Mean) ** 2
        + abs(CWeight1_4 - Vol1_Mean) ** 2 + abs(CWeight1_5 - Vol1_Mean) ** 2 + abs(CWeight1_6 - Vol1_Mean) ** 2
        + abs(CWeight1_7 - Vol1_Mean) ** 2) + abs(CWeight1_8 - Vol1_Mean) ** 2 + abs(CWeight1_9 - Vol1_Mean) ** 2
        + abs(CWeight1_10 - Vol1_Mean) ** 2) / 9)
    AREM1_Rd = round(AREM1_Math, 3)
    AREM1_Final = "Absolute Random Error of Measurement: " + str(AREM1_Rd) + "."
    AREM1_Form = Label(Top)
    AREM1_Form["text"] = AREM1_Final
    RRem1_Math = (100 * AREM1_Math) / Vol1_Mean
    RRem1_Rd = round(RRem1_Math, 3)
    RRem1_Final = "Relative Random Error of Measurement: " + str(RRem1_Rd)
    RRem1_Form = Label(Top)
    RRem1_Form["text"] = RRem1_Final

#Name_Results.pack()
    Title.pack()
    Pipette_SN.pack()
    Name_Form.pack()
    Date_Form.pack()
    Volume1_Form.place(x=15, y=150)
    Absolute1_Form.place(x=400, y=150)
    Relative1_Form.place(x=15, y=175)
    AREM1_Form.place(x=400, y=175)
    RRem1_Form.place(x=15, y=200)
   
# Define Form Variables
Name = Label(root, text="Tech Name")
Name_Entry = Entry(root)
Date = Label(root, text="Date")
Date_Entry = Entry(root)
Serial_Number = Label(root, text="Pipette Serial Number")
Serial_Entry = Entry(root)
Temperature = Label(root,
                    text="1. Enter the room temperature in °C to the nearest half degree. The required range is 15-30°C.",
                    justify=LEFT)
Room_Temp = Entry(root)
Humidity_Question = Label(root, text="2. Enter the humidity as a percent (%). The required range is >50%.")
Humidity = Entry(root)
Barometric_Question = Label(root, text="3. Enter the barometric pressure in inHG.")
Barometric_Pressure = Entry(root)
Convert = Button(root, text="Convert",
                 command=convert)

Instructions3 = Label(root, text="5. Record the correction factor here:")
Correction_Factor = Entry(root)
Instructions4 = Label(root, text="6. Place nonporous weighing container on the balance and tare the balance.\n" \
                                 "7. Place a new tip on the pipette.\n" \
                                 "8. Pre-wet the tip once. Hold the tip vertically and dip the tip into the water a few millimeters.\n" \
                                 "9. Slowly aspirate the water into the tip. Remove the tip from the water and remove any droplets on the outside of the tip.\n" \
                                 "10 Hold the filled tip against the side of the container and dispense the water.\n" \
                                 "11. Allow the display to stabilize and record the mass.\n" \
                                 "12. Repeat steps 6-11 10 times making sure each cycle is less that 60 seconds.",
                      justify=LEFT)
Select_Pipette = Label(root, text="Select the Pipette to be calibrated:")

#Pipette Options Selection List
Pipette_Options = [
        "Select",
        "Blue 50-1,000 µl",
        "Yellow 5-100 µl",
        "Purple 0.2 – 5 ml"
]
#Drop-Down for Pipettes
Clicked = StringVar()
Clicked.set(Pipette_Options[0])

Pipette_Drop = OptionMenu(root, Clicked, *Pipette_Options, command=selected)
Pipette_Drop.place(x=280, y=445)


Record_Instr = Label(root, text="Record each result in the block below to the nearest 0.0001.")
Record1 = Label(root, text="Max volume (mL):")
Record2 = Label(root, text="50% volume (mL):")
Record3 = Label(root, text="10% volume (mL):")
Weight1_1 = Entry(root)
Weight1_2 = Entry(root)
Weight1_3 = Entry(root)
Weight1_4 = Entry(root)
Weight1_5 = Entry(root)
Weight1_6 = Entry(root)
Weight1_7 = Entry(root)
Weight1_8 = Entry(root)
Weight1_9 = Entry(root)
Weight1_10 = Entry(root)
Weight2_1 = Entry(root)
Weight2_2 = Entry(root)
Weight2_3 = Entry(root)
Weight2_4 = Entry(root)
Weight2_5 = Entry(root)
Weight2_6 = Entry(root)
Weight2_7 = Entry(root)
Weight2_8 = Entry(root)
Weight2_9 = Entry(root)
Weight2_10 = Entry(root)
Weight3_1 = Entry(root)
Weight3_2 = Entry(root)
Weight3_3 = Entry(root)
Weight3_4 = Entry(root)
Weight3_5 = Entry(root)
Weight3_6 = Entry(root)
Weight3_7 = Entry(root)
Weight3_8 = Entry(root)
Weight3_9 = Entry(root)
Weight3_10 = Entry(root)
Analyze = Button(root, text="Analyze",
                 command=analyze)

# Place variables on the form
Name.place(x=15, y=40)
Name_Entry.place(x=100, y=40)
Date.place(x=300, y=40)
Date_Entry.place(x=350, y=40)
Serial_Number.place(x=550, y=40)
Serial_Entry.place(x=700, y=40)
Temperature.place(x=15, y=80)
Room_Temp.place(x=650, y=80)
Humidity_Question.place(x=15, y=120)
Humidity.place(x=475, y=120)
Barometric_Question.place(x=15, y=160)
Barometric_Pressure.place(x=300, y=160, width=100)
Image_Label.place(x=15, y=200)
Convert.place(x=410, y=165)
Instructions3.place(x=15, y=280)
Correction_Factor.place(x=280, y=280)
Instructions4.place(x=15, y=320)
Select_Pipette.place(x=15, y=445)
Record_Instr.place(x=15, y=480)
Record1.place(x=50, y=520)
Record2.place(x=400, y=520)
Record3.place(x=700, y=520)
Weight1_1.place(x=50, y=560, width=100)
Weight1_2.place(x=50, y=600, width=100)
Weight1_3.place(x=50, y=640, width=100)
Weight1_4.place(x=50, y=680, width=100)
Weight1_5.place(x=50, y=720, width=100)
Weight1_6.place(x=150, y=560, width=100)
Weight1_7.place(x=150, y=600, width=100)
Weight1_8.place(x=150, y=640, width=100)
Weight1_9.place(x=150, y=680, width=100)
Weight1_10.place(x=150, y=720, width=100)
Weight2_1.place(x=400, y=560, width=100)
Weight2_2.place(x=400, y=600, width=100)
Weight2_3.place(x=400, y=640, width=100)
Weight2_4.place(x=400, y=680, width=100)
Weight2_5.place(x=400, y=720, width=100)
Weight2_6.place(x=500, y=560, width=100)
Weight2_7.place(x=500, y=600, width=100)
Weight2_8.place(x=500, y=640, width=100)
Weight2_9.place(x=500, y=680, width=100)
Weight2_10.place(x=500, y=720, width=100)
Weight3_1.place(x=700, y=560, width=100)
Weight3_2.place(x=700, y=600, width=100)
Weight3_3.place(x=700, y=640, width=100)
Weight3_4.place(x=700, y=680, width=100)
Weight3_5.place(x=700, y=720, width=100)
Weight3_6.place(x=800, y=560, width=100)
Weight3_7.place(x=800, y=600, width=100)
Weight3_8.place(x=800, y=640, width=100)
Weight3_9.place(x=800, y=680, width=100)
Weight3_10.place(x=800, y=720, width=100)
Analyze.place(x=15, y=760)

root.mainloop()

1 Ответ

0 голосов
/ 05 августа 2020

Нет необходимости определять / ссылаться на глобальную переменную Volume1 в вашей analyze() функции, поскольку вы не меняете значение этой предварительно определенной глобальной переменной, вместо этого вы просто извлекаете значение.

Обратите внимание, что в tkinter для получения значения метки вы используете cget() вместо get(), поэтому ваш код будет Absolute_Error_1 = float(Volume1.cget("text"))

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...