mercredi 18 août 2021

"Variable is not defined" when trying to assign checkbox to an action with Tkinter

I'm new to Python or programming . I'm trying to bind tkinter checkbox to a command but have issues .

I have created an app that should save data to xlsx file based on the user input . When the user choose the relevant options they should be "Activated" however I get an error when I click on the relevant checkbox (Assigned command to one for now just to check if it works).

Below is my entire code and below the code I have added the error I get . I also add the GIU window so you see how it looks and what I mean exactly .

I found one or two threads discussing this issue however I don't think the source of the issue is the same . I'm using Python 3.9 with Anaconda and Spyder

The relevant code : .

  from tkinter import *
import tkinter as tk
from tkinter import filedialog
from termcolor import cprint ,colored
import pydicom
import pandas as pd




class gettingDataFromDICOM:
    def DICOMData(dcmread):
        pass
    #Asking the user to choose input location
    print('Welcome to DTETool v1.1')
    

def filelocainput():
    global Data
    filelocationinput = filedialog.askopenfilename()
    Data = pydicom.dcmread(filelocationinput , force = True )
    



    print('PatientID -',Data.PatientID)
    
    
    df = pd.DataFrame ({'PatientID ': [Data.PatientID]})

class tkgui:
    pass

def activate():
  if   Data.PatientID.get() == 1:
            scale.config(state=ACTIVE)
  elif Data.PatientID.get() == 0:
            scale.config(state=DISABLED)
     


Checkboxes =Tk()
Checkboxes.geometry('400x320')
TitleSelDCM = Label(Checkboxes , text ='DICOM fields selection',fg='white')
TitleSelDCM.place(y=0 ,x=120)
CheckboxWindowbg = PhotoImage(file="C:\Tools\DTETool\SeconderyBG.png")
CheckboxWindowbg_label = Label(Checkboxes , image=CheckboxWindowbg)
CheckboxWindowbg_label.place(y=0,x=0 , relwidth =1 , relheight=1)


CheckBoxPID = IntVar()
CheckBoxPID = Checkbutton(Checkboxes , text = 'PatientID', bg = '#0B5ED8' ,fg = 'white', onvalue = 1 , offvalue = 0  , variable = CheckBoxPID ,command =activate)
CheckBoxPID.place(y=20 , x=20)
Checkboxes.mainloop()

The error :

  Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files\Spyder\pkgs\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Tools\DTETool\DTEToolv1.1\DTEtool.py", line 113, in activate
    if   Data.PatientID.get() == 1:
NameError: name 'Data' is not defined

An image with the relevant parts

Thank you !




Aucun commentaire:

Enregistrer un commentaire