0

i have made a simple GUI app, with tkinter and configparser, to store the values in my entry/text fields.

But i need help with something. I want to make the pogram assign a new ini file every time the user saves the input from the button and give the inifile a ID starting from 1 to infinite

So the user fill's all entry's and hits the save all information button. The gui must then generate a new inifile (1).

def saveConfig():
    filename = "config.ini"
    file = open(filename, 'w')
    Config = configparser.ConfigParser()
    Config.add_section('ORDERDATA')
    Config.set("ORDERDATA", "REKVIRENT", e1.get())
    Config.set("ORDERDATA", "MODTAGER", e2.get())
    Config.set("ORDERDATA", "PATIENTFORNAVN", e3.get())
    Config.set("ORDERDATA", "PATIENTEFTERNAVN", e4.get())
    Config.set("ORDERDATA", "CPR", e7.get())
    Config.set("ORDERDATA", "DOKUMENTATIONSDATO", e5.get())
    Config.set("ORDERDATA", "ØNSKET UNDERSØGELSE", e6.get())
    Config.set("ORDERDATA", "ANAMNESE", t1.get('1.0', END))
    Config.set("ORDERDATA", "INDIKATION", t2.get('1.0', END))
    Config.write(file)
    file.close()
Martin Led
  • 65
  • 2
  • 2
  • 9

1 Answers1

0

If you want your program to save all your configuration files with ascending numbers, you could do the following:

# Python 2.7
import os
import ConfigParser as cp
import Tkinter as tk

def saveConfig():
    config = cp.ConfigParser()
    config.add_section("ORDERDATA")
    config.set("ORDERDATA", "REKVIRENT", e1.get())
    # Set all your settings here
    # Using os.listdir(), you can get the files in a folder in a list
    list_files = os.listdir(os.getcwd())
    # You can then convert the names of the files into integers for all
    # .ini files
    list_numbers = [int(x[:-4]) for x in list_files if x.endswith(".ini")]
    # If the length of this new list is 0, max will throw a ValueError
    if len(list_numbers) != 0:
        # Calculate the new file number by adding one to the highest found number
        new_file_num = max(list_numbers) + 1
    # To prevent the ValueError, set the number to 1 if no files are present
    else:
        new_file_num = 1
    # Derive the name of the file here
    new_file_name = str(new_file_num) + ".ini"
    # Open the file and write to it
    with open(new_file_name, "w") as file_obj:
        config.write(file_obj)


root = tk.Tk()
e1 = tk.Entry(root)
button = tk.Button(root, text="Click me!", command=saveConfig)
e1.pack()
button.pack()
root.mainloop()

For Python 3, you would only have to change the imports. Tested and working using Python 2.7 on Ubuntu.

RedFantom
  • 332
  • 1
  • 10
  • Hey RedFantom, Thank you so much ! it worked out great ! :) do you also now how to display the counter of the ini files i the GUI ? – Martin Led Feb 06 '17 at 10:15
  • You're ver welcome! ;) You could use a `tk.StringVar()` coupled to a `Label` with the `textvariable` option when creating the `Label`. Like so: `var = tk.StringVar()`, `lbl = tk.Label(root, textvariable=var)`, `var.set(str(number))`. The `Label` updates automatically in the `mainloop`. – RedFantom Feb 06 '17 at 11:37
  • @MartinLed If this answers your question, could you accept it as the answer? Thank you :) . – RedFantom Feb 12 '17 at 14:58
  • Hi RedFantom. of course, havent tried the last, with the label and tk.stringVar(). I have been busy with work. But ill try it out soon. But what du you mean by accepting the answer ? :) – Martin Led Feb 13 '17 at 07:11
  • @MartinLed StackOverflow works with questions and answers. When a question is answered, the answer for the question is marked with a green icon, as explained [here](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). This way, everyone can find the information in the future :) . Could you let me know whether the `tk.StringVar()` method works for you? – RedFantom Feb 14 '17 at 09:16
  • Hey RedFantom, Cant quite figure out how to make the stringvar work. Did like this : – Martin Led Feb 14 '17 at 17:07
  • var = StringVar() var.set(str()) L10 = Label(root, textvariable=var) L10.place(x=500, y=100) – Martin Led Feb 14 '17 at 17:08
  • Another idea, I have thought about. Is to save the ini file´s name, with the text entered in the entry. Lets say that I Have a entry, where i type my name. Then the ini file is saved in the folder with my name, or some other name, or number. – Martin Led Feb 14 '17 at 17:25
  • @MartinLed What exactly is not working? Did you use just `var.set(str())` or did you put an argument in `str()`? It should work, by updating the `StringVar` by using `var.set()` again. You can find more about `Tkinter.Entry` [here](http://effbot.org/tkinterbook/entry.htm), but if you cannot get it to work, you should open a new question. – RedFantom Feb 15 '17 at 17:33