Home > front end >  Can you create a generic Tkinter app that takes a .txt file of widget specifications?
Can you create a generic Tkinter app that takes a .txt file of widget specifications?

Time:02-14

I have just began coding with tkinter in Python and was wondering if there was a way to create a generic Tkinter driver with a root and mainloop that can read in a text file or maybe even a JSON or something that can populate the window with widgets based on said file.

For example, a text file with a structure like:

Frame: row=0; column=0, padx=2, pady=2;
Label: row=0, column=0, padx=2, pady=2, text=Some Text;
Combobox: row=1, column=0, padx=2, pady=2, values=('val1', 'val2'), variable=my_var;
Button: row=2, column=0, padx=2, pady=2, command=my_func(): print(my_var.get())

CodePudding user response:

Other toolkits like Qt and GTK3 have this built-in, but not tkinter. It is something that came into fashion long after tk was created, when GUI-builders became a thing.

Would it be possible to write something like this? Yes.

Personally, I would not think this so convenient. Because the file containing the description of the layout would have to be distributed with the application and put in a place where the application can find it. That is tricky to implement in a way that works on multiple platforms. Look at the sysconfig module; there are several places where you could store things depending on the platform. Some of those would require elevated permissions to install files there.

In fact, for one of my small GTK3 programs (gtk-razer), I even compressed the .glade file and ran it through base85encode so I could include it in my script. Basically this was done to avoid that problem!

  • Related