Python Forum
[Tkinter] passing value from tkinter entry to create xml document
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] passing value from tkinter entry to create xml document
#1
I have a below program. The problem is if i pull the text value from the tkinter entrybox "dbnameentry.get()" and pass it the create the root element of the xml document " top = ET.Element(dbnameentry.get())" then I get the XML parse error while parsing the created file.
Error-> xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1

However if I pass the manual value like "top = ET.Element('Students')" then the while parsing i do not get error. I commented this line in the below program.

I need help if there is any known issue or any issue in program itself.

Program Sample:
#################################################
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk, filedialog, messagebox
import xml.etree.ElementTree as ET

def create_db_window():
    directory_path = filedialog.askdirectory()
    def create_db():
        if not dbnameentry.get():
            messagebox.showerror("Error", "Please enter filename")
        elif not dbpwdentry.get():
            messagebox.showerror("Error", "please set password")
        else:
            top = ET.Element(dbnameentry.get())
            # top = ET.Element('Students')
            app = ET.SubElement(top, "app")
            studentname = ET.SubElement(app, "studentname")
            studentname.text = "Superman"
            studentid = ET.SubElement(app, "ID")
            studentid.text = "87"
            studentclass = ET.SubElement(app, "class")
            studentclass.text = "programming"
            try:
                tree = ET.ElementTree(top)
                tree.write(f'{directory_path}/{dbpwdentry.get()}.xml')
                messagebox.showinfo("Success", "Database created")
            except Exception as e:
                messagebox.showerror("Error", f"Failed to save file:\n{e}")

    createdbwindow = Toplevel()
    createdbwindow.geometry("350x160")
    dbnamelabel = Label(createdbwindow, text="Enter Db Name:")
    dbnamelabel.pack()
    dbnameentry = Entry(createdbwindow)
    dbnameentry.pack()
    dbpwdlabel = Label(createdbwindow, text="Set DB Password:")
    dbpwdlabel.pack()
    dbpwdentry = Entry(createdbwindow, show="*")
    dbpwdentry.pack()
    dbcreatebutton = Button(createdbwindow, text="Create", command=create_db)
    dbcreatebutton.pack()
mainwindow = Tk()
mainwindow.geometry('200x200')
createwindowbutton = Button(mainwindow, text="Create new window", command=create_db_window)
createwindowbutton.pack()
mainwindow.mainloop()
#############################################################
Gribouillis write Jul-24-2025, 10:08 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
It is full program code which can reproduce the issue. Please check my explanation. the Output error i mention in the explanation. Pasting again

Output error:
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 1
Reply
#3
It works fine for me running python 3.13 on a Windows 10 computer.

I ran the program, pressed the button and selected a folder. In the window that appeared I entered "test_database" for the db name field and "password" for the password field. The program creates an XML file named "password" that contains:
Quote:<test_database><app><studentname>Superman</studentname><ID>87</ID><class>programming</class></app></test_database>
Reply
#4
(Jul-24-2025, 06:50 PM)deanhystad Wrote: It works fine for me running python 3.13 on a Windows 10 computer.

I ran the program, pressed the button and selected a folder. In the window that appeared I entered "test_database" for the db name field and "password" for the password field. The program creates an XML file named "password" that contains:
Quote:<test_database><app><studentname>Superman</studentname><ID>87</ID><class>programming</class></app></test_database>


Hi deanhystad , Thanks for your reply , I also have python 3.13.5 and windows 10.
However not working for me when line 16 un-commented and line 17 commented. If i reverse it then it works.

Regards.
Reply
#5
I ran the program exactly as posted with line #17 commented out. This time I entered "student" as the db name. The generated XML file:
Quote:<student><app><studentname>Superman</studentname><ID>87</ID><class>programming</class></app></student>
You can see that the program used the name from dbnameentry, "student", and not the hardcoded entry from line #16 "Students".

I ran the code from the CMD shell, powershell, VSCode and IDLE, and it always works. I also ran it on windows 11 with no problem. Are you getting an error message and a traceback? If so, post please.
Reply
#6
can you checked the parsing is working of the created file. Below is the parsing code.
import xml.etree.ElementTree as ET
tree = ET.parse('created_file_path_with previous_code')
root = tree.getroot()
print(root[0][0].text + " " + root[0][0].tag)
print(root[0][1].text + " " + root[0][1].tag)
print(root[0][2].text + " " + root[0][2].tag)
Weird thing is now when i ran the program to collect entire traceback , it is not giving an error now.
buran write Jul-28-2025, 03:48 PM:
Please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
Reply
#7
I ran the code from your last post with the xml file created by your first post. It worked fine. However, I can get your error if I type a space somewhere in for the dbname entry. Maybe that was what you were doing, entering a string that was not a valid xml tag.
Reply
#8
This makes it easy to generate structured data directly from GUI input fields. The approach is useful for applications where users provide details that need to be stored, shared, or processed in XML format. It’s a straightforward way to connect a simple interface with flexible data Link Removed
Gribouillis write Aug-28-2025, 11:47 AM:
Clickbait link removed. Please read What to NOT include in a post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Help create scrollbar in chatbot with tkinter on python fenec10 6 7,284 Aug-28-2025, 10:38 AM
Last Post: fazanbabar82
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 4,005 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 7,925 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  [Tkinter] Update variable using tkinter entry methon drSlump 6 10,165 Oct-15-2021, 08:01 AM
Last Post: drSlump
  Tkinter | entry output. Sap2ch 1 3,134 Sep-25-2021, 12:38 AM
Last Post: Yoriz
  .get() from generated Entry widgets in tkinter snakes 4 10,326 May-03-2021, 11:26 PM
Last Post: snakes
  Entry Validation in tkinter shahulvk 4 23,159 Oct-28-2020, 10:12 PM
Last Post: joe_momma
  Tkinter: Create an homepage look like PeroPuri 8 10,823 Jun-26-2020, 12:57 AM
Last Post: menator01
  Create image on a Toplevel using tkinter ViktorWong 3 11,169 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Getting Input from Tkinter Entry juliabrushett 6 28,238 May-30-2020, 03:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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