I thought to this idea to speed up writing code with tkinter, so that you do not have to remember all the stuff you have to write to make the parameters for you widgets to be right. This is the start to make a more complex helping app for coding tkinter GUI. Let’s see it.
import tkinter as tk # WIDGETS STRINGS entry_string = """ display1 = tk.StringVar() entry1 = tk.Entry( relief=tk.FLAT, textvariable=display1, justify='right', bg='orange') entry1.pack() entry1["font"] = "arial 30 bold" """ counter = 1 def entry_code(): global entry_string, counter, display1 entry_string2 = entry_string.replace("display1", "display" + str(counter)) entry_string2 = entry_string2.replace("entry1", "entry" + str(counter)) text.insert(tk.END, entry_string2) counter += 1 root = tk.Tk() frame1 = tk.Frame(root) frame1.pack(side="left") b1 = tk.Button(frame1, text="entry", command=entry_code) b1.pack(side="left") frame2 = tk.Frame(root) frame2.pack(side="left") text = tk.Text(frame2) text.pack() root.mainloop()
This is the output
So, if you press one time you got your snippet example. Moreover, if you press 2 or more, you got the other widgets with their own numbered name, comprised the StringVar… quite cool. You could male a for loop for this, but this is kinda fast and a zero brainer too.
If we could make it for all the widgets it could be a nice tool…. maybe in the next days.
Video about this code to copy and paste tkinter widgets code
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame