Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Kako uraditi ovo u Pythonu?

[es] :: Python :: Kako uraditi ovo u Pythonu?

[ Pregleda: 16443 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

wraith46

Član broj: 336363
Poruke: 29



+1 Profil

icon Kako uraditi ovo u Pythonu?08.04.2017. u 21:55 - pre 91 meseci
Code:
void SaveAll()
{

    XmlDocument xDoc = new XmlDocument();

    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
    string vocabulary_path = path + "\\Vocabulary\\Words.xml";

    xDoc.Load(vocabulary_path);
    XmlNode xNode = xDoc.SelectSingleNode("Words");
    xNode.RemoveAll();
    foreach (Word w in words)
    {
        XmlNode xTop = xDoc.CreateElement("Word");
        XmlNode xWord = xDoc.CreateElement("Word");
        XmlNode xExplanation = xDoc.CreateElement("Explanation");
        XmlNode xTranslation = xDoc.CreateElement("Translation");
        XmlNode xExamples = xDoc.CreateElement("Examples");

        xWord.InnerText = w.WordOrPhrase;
        xExplanation.InnerText = w.Explanation;
        xTranslation.InnerText = w.Translation;
        xExamples.InnerText = w.Examples;
        xTop.AppendChild(xWord);
        xTop.AppendChild(xExplanation);
        xTop.AppendChild(xTranslation);
        xTop.AppendChild(xExamples);
        xDoc.DocumentElement.AppendChild(xTop);
    }

    xDoc.Save(vocabulary_path);
    Sync();

}
 
Odgovor na temu

jablan

Član broj: 8286
Poruke: 4541



+711 Profil

icon Re: Kako uraditi ovo u Pythonu?09.04.2017. u 17:22 - pre 91 meseci
Šta si uradio dosad?
 
Odgovor na temu

wraith46

Član broj: 336363
Poruke: 29



+1 Profil

icon Re: Kako uraditi ovo u Pythonu?09.04.2017. u 17:55 - pre 91 meseci
Napravio sam za sebe jednu aplikaciju u C# koju koristim za ucenje engleskog. E sad, prebacio sam se na Linux i pokusao sam da tu aplikaciju run-ujem preko Mono-a, medjutim ne radi mi bas najbolje pa sam resio da je napravim u Pythonu (ucim Python, spremam se za fax). Uradio sam oko 50% posla - napravio sam GUI i nekoliko metoda, ali zapeo sam kod loadovanja/save-ovanja elemenata iz liste u/iz XML, uopste ne znam odakle da krenem. Mislim da treba koristiti klasu ElementTree, ali ni u to nisam siguran. Ako moze pomoc, ili neki link... Hvala unapred

Code:
# Vocabulary.py
# GUI program to manage unknown words

from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import xml.etree.ElementTree as ET


class Word:

    def __init__(self, wordorphrase, explanation, translation, example):
        self.wordorphrase = wordorphrase
        self.explanation = explanation
        self.example = example
        self.translation = translation

class Vocabulary(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)
        self.master = master
        self.master.resizable(width = False, height = False)
        self.master.title("Vocabulary")
        self.create_widgets()
        self.words = []

    def create_widgets(self):

        self.buttons_frame = Frame(self.master)
        self.buttons_frame.grid(row = 10, sticky = W)

        self.search_frame = Frame(self.master)
        self.search_frame.grid(row = 1, sticky = W, columnspan = 2)

        self.comboBox = ttk.Combobox(self.search_frame,
                                     width = 3)
        self.comboBox.grid(row = 0, column = 14, sticky = W)
        self.comboBox['values'] = ( 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' )

        self.btn_Add = Button(self.buttons_frame,
                              text = 'Add',
                              command = self.add_item)
        self.btn_Add.grid(row = 0, sticky = W)

        self.btn_Remove = Button(self.buttons_frame,
                                 text = 'Remove',
                                 command = self.remove_item)

        self.btn_Remove.grid(row = 0, column = 1, sticky = W)

        self.btn_Edit = Button(self.buttons_frame,
                               text = 'Edit',
                               command = self.edit_item)
        self.btn_Edit.grid(row = 0, column = 2, sticky = W)

        self.btn_Save = Button(self.buttons_frame,
                               text = 'Save',
                               command = self.save_item)
        self.btn_Save.grid(row = 0, column = 3, sticky = W)

        self.btn_Refresh = Button(self.buttons_frame,
                               text = 'Refresh',
                               command = self.refresh_all)
        self.btn_Refresh.grid(row = 0, column = 4, sticky = W)

        self.lblSearch = Label(self.search_frame, text = 'SEARCH: ')
        self.lblSearch.grid(row = 0, column = 5, sticky = W)

        self.txt_Search = Text(self.search_frame,
                               height = 1,
                               width = 70)
        self.txt_Search.grid(row = 0, column = 6, columnspan = 3, sticky = W)

        self.lblWordsOrPhrases = Label(self.master, text = 'WORDS/PHRASES:')
        self.lblWordsOrPhrases.grid(row = 2, column = 0)

        self.lblWordOrPhrase = Label(self.master, text = 'Word or phrase:')
        self.lblWordOrPhrase.grid(row = 2, column = 1, sticky = W)

        self.listBox = Listbox(self.master,
                               selectmode='multiple',
                               height = 34,
                               width = 38)
        self.listBox.grid(row = 3, column = 0, rowspan = 7, sticky = W)

        self.txt_WordOrPhrase = Text(self.master,
                                height = 1,
                                width = 40)
        self.txt_WordOrPhrase.grid(row = 3, column = 1, sticky = N)

        self.lblExplanation = Label(self.master, text = 'Explanation:')
        self.lblExplanation.grid(row = 4, column = 1, sticky = W)

        self.txt_Explanation = Text(self.master,
                                    height = 10,
                                    width = 40)
        self.txt_Explanation.grid(row = 5, column = 1, sticky = N)

        self.lblTranslation = Label(self.master, text = 'Translation:')
        self.lblTranslation.grid(row = 6, column = 1, sticky = W)

        self.txt_Translation = Text(self.master,
                               height = 10,
                               width = 40)
        self.txt_Translation.grid(row = 7, column = 1, sticky = N)

        self.lblExamples = Label(self.master, text = 'Example(s):')
        self.lblExamples.grid(row = 8, column = 1, sticky = W)

        self.txt_Example = Text(self.master,
                                height = 10,
                                width = 40)
        self.txt_Example.grid(row = 9, column = 1, sticky = S)

    def get_word(self):
        return self.txt_WordOrPhrase.get('1.0', '1.0 lineend')

    def get_explanation(self):
        return self.txt_Explanation.get('1.0', '1.0 lineend')

    def get_translation(self):
        return self.txt_Translation.get('1.0', '1.0 lineend')

    def get_example(self):
        return self.txt_Example.get('1.0', '1.0 lineend')

    def add_item(self):

        w = Word(self.get_word(), self.get_explanation(), self.get_translation(), self.get_example())

        self.words.append(w)

        self.listBox.insert(END, w.wordorphrase)

        self.clear_all()

    def remove_item(self):

        self.listBox.delete(ANCHOR)

    def edit_item(self):
        pass

    def save_item(self):
        pass

    def save_all(self):
        self.tree = ET.parse('Words.xml')
        self.root = self.tree.getroot()


    def clear_all(self):
        self.txt_WordOrPhrase.delete('1.0', END)
        self.txt_Explanation.delete('1.0', END)
        self.txt_Translation.delete('1.0', END)
        self.txt_Example.delete('1.0', END)

    def refresh_all(self):
        pass

    def on_closing(self):
        if messagebox.askokcancel("Quit", "Do you want to quit?"):
            self.save_all
            self.master.destroy()

def main():
    root = Tk()
    gui = Vocabulary(root)
    root.protocol("WM_DELETE_WINDOW", gui.on_closing)
    root.mainloop()

if __name__ == '__main__':
    main()
 
Odgovor na temu

wraith46

Član broj: 336363
Poruke: 29



+1 Profil

icon Re: Kako uraditi ovo u Pythonu?12.04.2017. u 16:04 - pre 91 meseci
Finalno resenje:

Code:
    def save_all(self):

        path = os.path.expanduser('~/Desktop')
        vocabulary = os.path.join(path, 'Vocabulary', 'Words.xml')

        tree = ET.ElementTree(file=vocabulary)

        for xNode in tree.getroot().findall('Word'):
            tree.getroot().remove(xNode)

        for w in self.words:
            xTop = ET.Element('Word')
            xWord = ET.Element('Word')
            xExplanation = ET.Element('Explanation')
            xTranslation = ET.Element('Translation')
            xExamples = ET.Element('Examples')

            xWord.text = w.wordorphrase
            xExplanation.text = w.explanation
            xTranslation.text = w.translation
            xExamples.text = w.example

            xTop.append(xWord)
            xTop.append(xExplanation)
            xTop.append(xTranslation)
            xTop.append(xExamples)

            tree.getroot().append(xTop)

        tree.write(vocabulary)



[Ovu poruku je menjao wraith46 dana 13.04.2017. u 16:28 GMT+1]
 
Odgovor na temu

[es] :: Python :: Kako uraditi ovo u Pythonu?

[ Pregleda: 16443 | Odgovora: 3 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.