Python Forum
[Tkinter] how to export a dictionary from a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] how to export a dictionary from a class
#1
I'm trying to convert an app from a top down program to classes and an having a hard time finding out how to export a dictionary to get something besides the object address. I have seen something like this one time and can not go back and find it again.

What I am doing is downloading a dictionary from an API and want to make it available to my tkinter application class. Presently have have the class to download the dictionary outside of the main application class and want it available to several sub-classes. I've been led to believe this is the correct way to approach the problem, keep classes not in the normal work flow outside of the app class.

This probably hasn't been the best description of the problem but I'm still trying to learn Tkinter and classes at the same time.

Thanks for any suggestions.

Ron R
Reply
#2
Where you get the dictionary from is your business. Once you have it, why not save it as a .json? That way, it is always available for use.

This uses a dictionary from another question here, saves it as json, then you can load it again any time, edit it, add or subtract from it, then save again for later use.

import json

products = {
   "one": {"name": "Laptop", "price": {"amount": 999, "currency": "USD"}},
   "two": {"name": "Smartphone", "price": {"amount": 499, "currency": "USD"}},
   "three":{"name": "Tablet", "price": {"amount": 299, "currency": "EUR"}},
    "four": {"name": "Monitor", "price": {"amount": 199, "currency": "USD"}},
    "five": {"name": "Camera", "price": {"amount": 599, "currency": "EUR"}},
    "six": {"name": "Cat Camera", "price": {"amount": 699, "currency": "EUR"}},
    "seven": {"name": "Baby Monitor", "price": {"amount": 399, "currency": "USD"}},
    "eight": {"name": "Laptop dancer", "price": {"amount": 9999, "currency": "USD"}},
    "nine": {"name": "Dumbphone", "price": {"amount": 1, "currency": "RMB"}},
    "ten": {"name": "Poison Tablet", "price": {"amount": 1555, "currency": "EUR"}},
}

# make a string of products
# dict2json = json.dumps(products, indent=4)

# save the dict as json for later use
# indent=4 just makes the text look pretty in a text editor, is irrelevant
savejson = '/home/peterr/temp/mydata.json'
with open(savejson, "w") as outfile:
    json.dump(products, outfile, indent=4)

# load the json again for use 
with open(savejson, 'r', encoding='utf-8') as infile:
    data = json.load(infile)

type(data) # returns <class 'dict'>
data['ten'] # returns {'name': 'Poison Tablet', 'price': {'amount': 1555, 'currency': 'EUR'}}
Reply
#3
What do you mean by "export a dictionary". Pedroski55 thinks you are asking how to serialize a dictionary, write it to a file in a form that you can read later. If that's what you want to do, writing the dictionary to a json file will work IF the dictionary only contains strings, floats and ints, or other dictionaries, or lists, that only contain those types of values. If you want to write any other kind of information to a json file it must first be converted to something that only uses json supported data types, and when you read the file additional processing might be required to get the data in the form you want.

Another way to write a dictionary to a file and have another file read is to use pickle. Pickle is much more flexible about what it can read/write, but the file is binary and not human readable.

https://docs.python.org/3/library/pickle.html

"export" is not a word I would use to describe saving a dictionary in a file. If your question is an extension of your tkinter layout question, I think it likely that you are talking about getting weather data as a dictionary from your API and want to pass information from the dictionary, or the entire dictionary to classes that will present the weather info in a tkinter window. Is that what you are asking?
Reply
#4
Sorry, maybe you meant something like this:

class2dict

Stiil sticking with json:

import json
from dataclasses import dataclass, asdict


@dataclass
class Movie:
    title: str
    director: str
    year: int


movie = Movie("The Dark Knight", "Christopher Nolan", 2008)
movie_dict = asdict(movie)
movie_json = json.dumps(movie_dict)
print(movie_json)
Returns:

Output:
{"title": "The Dark Knight", "director": "Christopher Nolan", "year": 2008}
Reply
#5
Hi,

in addition to @deanhystad's post: do you mean by "exporting" making the dict available within your code to other classes, modules, parts of the code or does "exporting" mean writing it to disc, send it over some type of protocol like e.g. HTTP to another machine or do you mean something totally different?

Regards, noisefloor
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020