#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    82
    Rep Power
    1

    [Python 3.x Tkinker] Overlap Window Function not works..., Error Found!


    Hey GUI Programmers...

    I try to create multiple windows thad make an tool thad must be possible in the "new = Tk" window
    and creates an label to the "root = Tk" window, but if you use my tool (just i create an simple
    test to make multiple windows with overlapped tools), i get an error...

    So i get two scripts... one Run script and one Script, called "Mat", in the "Scripts" folder, imported
    "from Scripts.Mat import *" (* = only the script "New")...

    This is my Run Script, called "Run.py"...:
    Code:
    # Import Tkinter to my Script
    from tkinter import *
    from Scripts.Mat import *
    
    # Make an Main App
    def main():
    
        # Setup App
        root = Tk()
        root.geometry('640x480')
        root.title('Menubar Test')
        root.config(background = 'black')
    
        # Print (File Menu - Tools)
        def OpenNew():
            New()
    
        # Make an Basic-Menubar for all Menu's
        menu = Menu(root, foreground = 'white', background = 'blue', relief = SUNKEN)
        root.config(menu = menu)
    
        # Make an File-Menu (First Command called before my First Seperator for my File Menu)
        file = Menu(menu, foreground = 'white', background = 'blue', activeforeground = 'gray', activebackground = 'cyan', tearoff = False)
        file.add_command(label = 'New', command = OpenNew)
        menu.add_cascade(label = 'File', menu = file)
    
        # App Mainloop
        root.mainloop()
    
    # Check of __name__ __main__ is and start my app if it __main__ is the name
    if __name__ == '__main__':
        main()

    If you can see.... i get setup my Run app with "root = Tk, ... at last root.mainloop"
    and get my second script imported, called "from Scripts.Mat import *" (* is my definded function New())...

    And this is my other script, called "Mat"..., with my definded function "New()"...
    Code:
    from tkinter import *
    
    def New():
        new = Tk()
        new.geometry('640x480')
        new.title('New File')
        new.config(background = 'black')
    
        def AddLabel():
    
            l = Label(root, text = 'Label', foreground = 'white', background = 'purple')
            l.pack(fill = X, side = TOP)
            
        b = Button(new, text = 'Add Label', foreground = 'white', background = 'blue', activeforeground = 'gray', activebackground = 'cyan', command = AddLabel)
        b.pack(fill = X, side = TOP)
        
        new.mainloop()

    The means of my second script (or other script, imported in the run script), is thad
    if you press the "File > New" button in my called menu... thad you get my window,
    programmed in my second script (called new = Tk())...

    But in this window, you get an button to create an label from the "new" window (called
    new = Tk()) to the "root" window, called "root = Tk()".....

    But if you use my tool (it is just an test app, just i can figure out to build tool-overlaps
    in python and tkinter), you get an error...

    This is my error, called in my "new = Tk()" window, in my second script...:
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\Bar\Scripts\Mat.py", line 11, in AddLabel
    l = Label(root, text = 'Label', foreground = 'white', background = 'purple')
    NameError: name 'root' is not defined
    Can anyone help me to fix this problem with my tool overlap in python 3.x and tkinter ?...
    Only fix this problem, pleace ?...

    Thanks for help, Jamie.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    667
    Rep Power
    40
    Two instances of Tk() do not play nice with each other. Instead pass root to New() and use a Toplevel The Tkinter Toplevel Widget so
    Code:
    New(root)
    ##
    ## and
    def New(root):
        new = Toplevel(root)
    Also, since the Label has root for a parent, the Button to call the function to add the Label, and the function that adds the Label should be in the first program.
    Last edited by dwblas; March 25th, 2018 at 10:17 PM.

IMN logo majestic logo threadwatch logo seochat tools logo