T
T
takecake2021-07-14 07:34:30
Python
takecake, 2021-07-14 07:34:30

How to make a dictionary global?

I don't know Python well, but I try to learn it.
The fact is that the function does not see the dictionary that I declared outside the function, it is understandable, but how to declare this dictionary global in order to update, call and view it from any function and beyond?

active_list = {}

  def on_click(e,h,w):
    e.pushButton.setStyleSheet('color: white; background-color: #4460F1; border-radius: 5px; border: 1px solid #4460F1;')
    # D1Standart(int(w),int(h))
    active_list['DPM1'] = 1
    print(active_list)

active_list['DPM1'] = 1
NameError: name 'active_list' is not defined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergo Zar, 2021-07-14
@takecake

active_list = {}

  def on_click(e,h,w):
    global active_list
    e.pushButton.setStyleSheet('color: white; background-color: #4460F1; border-radius: 5px; border: 1px solid #4460F1;')
    # D1Standart(int(w),int(h))
    active_list['DPM1'] = 1
    print(active_list)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question