-1

I'm making a program to ease my work. The concept of it is that it has some company names (I'll make a button to add more of them) and stores the info when and what loan they took.

So basically I open the program, it displays the company names, I click on them and it opens a window of their taken loans (also going to make a button to add more loans at different dates). I just want to make the program to store the company names and what loans they took which I can add more and more of them.

I'm going to add the info through some simple entry dialogs.

How is it possible to store the info in the program itself when I press a button? Like a small window to pop up with the info?

pnuts
  • 58,317
  • 11
  • 87
  • 139
  • 2
    Store the data in a file using JSON for example or read up on some tutorials on using SQLite (*part of the Python standard library*). – James Mills Jun 13 '15 at 09:11
  • 5
    You don't want to store it in the code. You want a configuration file that you can manipulate. Look at markup languages and JSON. Also, look up GUI frameworks. – Alex Huszagh Jun 13 '15 at 09:11
  • I'll think that this is a duplicated of [Python Storing Data](http://stackoverflow.com/questions/27913261/python-storing-data) –  Jun 13 '15 at 09:13
  • So if i use JSON where exactly the info will store? And why is it bad to store in the code? – isuckatpython Jun 13 '15 at 09:21
  • There's a lot of good reasons. 1). Assume you'll be updating the stored data. If it's in the program body, you corrupt the program if you have a bug. Which then could erase or corrupt... everything. 2). A lot of text editors have code intelligence. You will waste a lot of performance in it indexing and referencing your megabytes of stored data so it can feed back to you a lot of useless data. 3). It isn't executable code. It's configurable data, and so it should be separated logically to configure the parameters to run the code as need be. – Alex Huszagh Jun 13 '15 at 09:33
  • As far as how JSON will store the data, you can read up on the JSON specifications here: http://json.org/ And you can read about Python's JSON module here: https://docs.python.org/2/library/json.html – Alex Huszagh Jun 13 '15 at 09:36
  • As far as i checked JSON it seems pretty difficult. Do you know any basic tutorials how i could implement it into my code and make a really simple text database? – isuckatpython Jun 13 '15 at 09:43
  • JSON is simple. It is much simpler to learn the basics of programming prior to trying to implement a graphic user interface with no knowledge of how to code. Take a Python tutorial. Learn object-oriented programming in Python. Learn about dictionaries, lists, and basics data structures in Python. Learn JSON. Practice. Then write your program. Or you will struggle because you will not have the background knowledge to know when you are doing something wrong on why it is wrong, – Alex Huszagh Jun 13 '15 at 09:46
  • Thank you for your answers. Where exactly the database(info) will be? If i'll for example take my .exe program to an another computer, they'll be able to read the info? – isuckatpython Jun 13 '15 at 09:50
  • Wherever you define the path to be. This is all specified. Typically, in the same directory or a config directory next to your executable. – Alex Huszagh Jun 13 '15 at 10:04
  • 2
    Ohhh so i just link it from my code? Dude thank you a lot! I owe you much. – isuckatpython Jun 13 '15 at 10:11
  • Yeah, I can write a quick summary if you want. – Alex Huszagh Jun 13 '15 at 10:13
  • That'd be awesome if you have time. – isuckatpython Jun 13 '15 at 10:20

1 Answers1

0

For basics, you should check out the JSON library from Python: https://docs.python.org/2/library/json.html

Here's a quick example:

>>> import json
# grabs config.json in same directory as python file
>>> with open("config.json", "r") as e:
...     myconfig = json.load(e)
>>> print myconfig
{u'4': u'5', u'6': 7}
# dump to string representation
>>> config_string = json.dumps(myconfig)
>>> config_string
'{"4": "5", "6": 7}'
# load from text
>>> json.loads(config_string)
{u'4': u'5', u'6': 7}
# dump config to file
>>> with open("config.json", "w") as e:
...    json.dump(myconfig, e)

This looks in the current directory, and shows 2 ways of loading and dumping a JSON configuration. You should mainly be using the first and last one. Once loaded, a JSON object is translated to a Python dictionary, and it acts identically. Likewise, a Python dictionary comprised of dictionaries, lists, and strings will act identically to a JSON. The only difference is an integer or numerical key will be converted to a float.

As for GUI frameworks, I recommend PySide (Qt) or Tkinter. Qt is extensive, but has excellent, excellent documentation and is extremely rich in features, making it quite easy to extend your application as need be. However, as it as a C++ framework, the best documentation is written for the C++ version, however, there are excellent resources for PySide.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
  • Thank you, i'm using Tkinter by the way. – isuckatpython Jun 13 '15 at 10:27
  • TKinter is quite good, between it, wxPython, and PyQt, Python has excellent options available. – Alex Huszagh Jun 13 '15 at 10:32
  • 1
    Again, i don't need anything difficult. My whole program is just made of buttons and text. – isuckatpython Jun 13 '15 at 10:33
  • Yeah for your use, Tkinter or PyQt is definitely the way to go, since they're simple and well documented. It makes it a lot easier when you're just starting out, as well as when you're expanding your features (if you do). – Alex Huszagh Jun 13 '15 at 10:34
  • So thank you for all of this. Could I sometimes ask you if I have questions? Starting out programming is really hard for me, even though i've been doing for a long time now. – isuckatpython Jun 13 '15 at 10:38