Dec-15-2019, 04:18 AM
Quote:I do not understand how the "window_name.mainloop()" worksmainloop is a process that runs the program, then waits for a quit command from tkinter upon which it terminates, basically it's waiting for an event (interrupt).
Quote:Do I understand correctly or variables in functions only live in that function? I am struggling to have the "bypass" button to work - it would set a flag which would prevent the boiler from starting if set to TRUE - but if I understand correctly variables only live into each function independently?
This can be overcome forever, if you use classes, which are not trivial, and I can't explain in this post.
I can however do two things:
- Show you where to learn
- Provide an example -- Below
Here's an example that displays a virtual environment (or system) package list, and saves to a requirements.txt (or other name) file
that can be used for packaging
import tkinter as tk import tkinter.filedialog as fd from pip._internal.operations import freeze class InstalledPackages: def __init__(self, parent): self.w = parent self.bgc1 = '#ffffe5' self.bgc2 = 'Lavender' self.bgc3 = ' LightCyan' self.pkglist = None self.w.geometry('400x400+10+10') self.w.title("Larz60+ Python Package List") self.pkglist = freeze.freeze() self.t1 = None def list_packages(self): w = self.w self.t1 = tk.Text(w, wrap=tk.WORD, undo=0, bg=self.bgc1) self.t1.pack(expand=1, fill=tk.BOTH) self.t1scroll = tk.Scrollbar(self.t1) self.t1.configure(yscrollcommand=self.t1scroll.set) self.t1scroll.config(command=self.t1.yview) self.t1scroll.pack(side=tk.RIGHT, fill=tk.Y) b1 = tk.Button(w, text='Save', command=self.save_requirements_file) b1.pack(side=tk.BOTTOM) self.t1.insert(tk.END, "{Package Name} -- {Version Info}\n\n") for pkg in self.pkglist: self.t1.insert(tk.END, f" {pkg}\n") def save_requirements_file(self): fp = fd.asksaveasfile(mode='w', defaultextension=".txt") gui_text = str(self.t1.get(1.0, tk.END)) # Nothing to do if fp is None fp.write(f"{gui_text}") if __name__ == '__main__': root = tk.Tk() InsPkgs = InstalledPackages(root) InsPkgs.list_packages() root.mainloop()output (my virtual environment)
Note that all variables are stores and initialized in the __init__ method.
self.t1 is used across methods, in list_packages and save_requirements_file