0

I have a program called main.py:

def prog2():
exec(open("prog2.py").read())

buttonHMI = tk.Button(text="Program HMI", width=15, font=("Arial", 14), command=prog2)
buttonHMI.grid(column=0, row=3, columnspan=1, padx=20, pady=10, sticky=W)

So there's 1 button and when pressed it executes a 2nd program called prog2:

from tkinter import * 
from tkinter import ttk

window = tk.Tk()

def close():
    window.destroy

CB1 = Checkbutton(window, text="Enable VNC", font=("Arial", 12), variable=action_VNC)
CB1.grid(row=0, sticky=W, padx=10)
CB1.configure(bg='#34aeeb')

ButtonClose= tk.Button(window, text="Close", width=20, font=("Arial", 14), command=window.destroy)
ButtonClose.grid(columnspan=2, column = 3, row=9, sticky=W, padx=10, pady=10)

class Timer:
    def __init__(self, window=None):
        self.sv = tk.StringVar()
        
        ButtonShow = tk.Button(window, text="Program HMI", width=20, font=("Arial", 14), command=self.actions)
        ButtonShow.grid(columnspan=2, row=9, column = 0, sticky=W, padx=10, pady=10)

        progress = ttk.Progressbar(window, orient = HORIZONTAL, length = 500, mode = 'determinate')
        progress.grid(row=10, columnspan = 4, sticky=W, padx=10, pady=10)

        outText = Text(window, width=50, height=20, wrap=WORD)
        outText.grid(row=11, columnspan=4, padx=10)
        
        
###########################################################
#start actions.  
    def actions(self):
        print("action")


Timer()

window.mainloop()

The problem I'm having is from main.py, I open prog2, some of the objects (Buttonshow, progress, outText) will show up in the main.py window. But CB1 and Close objects will show in prog2. If I execute prog2.py by itself, then all the objects will show correctly in prog2 window. How do I get all of prog2 objects to show in prog2?

nearbyatom
  • 37
  • 4
  • 1
    Use `Timer(window)` instead. But multiple instances of `Tk()` should be avoided. – acw1668 Sep 14 '21 at 17:05
  • 1
    `exec(open("prog2.py").read())` smells like an `import` would have been the right thing... – Thierry Lathuille Sep 14 '21 at 17:14
  • you sure seond program runs ? CB1 = Checkbutton(window, text="Enable VNC", font=("Arial", 12), variable=action_VNC) NameError: name 'action_VNC' is not defined – pippo1980 Sep 14 '21 at 17:20
  • @pippo1980 - yes it works. I forgot to post that that function called action_VNC. It does work though. Biggest problem is I have some objects in Prog2 showing up in main.py. – nearbyatom Sep 14 '21 at 18:13
  • @acw1668 - Sorry for the noob question but how do I use Timer(window)? Something like 'def__int__(self, Timer(window)): ' or 'Class Timer(window)'? Won't I need multiple TK() if I wanted to make multiple windows in tkinter? Prog2.py started off as an independent program. Now I created main.py to run prog2.py and other "formerly independent" programs off of it. – nearbyatom Sep 14 '21 at 18:27
  • I think @acw1668 meant that you replace your current line of `Timer()` with `Timer(window)`. To create new windows use `Toplevel` widget. You should still use `import` for importing stuff from other python files – Matiiss Sep 14 '21 at 18:58
  • I am a noob too, both python and especially tkinter, but take a look at this https://stackoverflow.com/questions/17466561/best-way-to-structure-a-tkinter-application, upvote it if you find it interesting or https://docs.python.org/3/library/tkinter.html#the-window-manager – pippo1980 Sep 14 '21 at 19:21
  • @acw1668 - If I called multiple instances of tk() in the same file, I'd get that it's bad, but these are 2 separate files, is this still bad? – nearbyatom Sep 15 '21 at 13:01
  • @Matiiss - I did and I got a different error. I get a NameError: name 'window' is not defined. Basically I can run scripts off a 2nd python prog, but I can't run a "2nd instance" of tkinter? – nearbyatom Sep 15 '21 at 13:05

0 Answers0