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

    Join Date
    Oct 2017
    Posts
    85
    Rep Power
    2

    [Python 3 + Tkinter] Option Menu, Error Found!


    Hey Python Programmers...

    I try to create an option menu they create diffrence buttons... but i get an problem with it...

    This is an test to train my self in programming... this is my script:
    Code:
    from tkinter import *
    
    main = Tk()
    
    def selectOption(optionselect):
    
        global select
    
        select = optionselect
    
        select = 'Image'
    
        if select == 'Image':
            selectOption_Image()
            buttonTexture.config(text = 'Texture' % (buttonTexture, buttonTextureExt))
            buttonPreview.config(text = 'Preview' % (buttonPreview, buttonPreviewExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
        if select == 'Texture':
            selectOption_Texture()
            buttonImage.config(text = 'Image' % (buttonImage, buttonImageExt))
            buttonPreview.config(text = 'Preview' % (buttonPreview, buttonPreviewExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
        if select == 'Preview':
            selectOption_Preview()
            buttonImage.config(text = 'Image' % (buttonImage, buttonImageExt))
            buttonTexture.config(text = 'Texture' % (buttonTexture, buttonTextureExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
    
        if select == 'Export':
            selectOption_Export()
            buttonImage.config(text = 'Image' % (buttonImage, buttonImageExt))
            buttonTexture.config(text = 'Texture' % (buttonTexture, buttonTextureExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
    
        def selectOption_Image():
    
            def selectOption_Image_Button():
    
                def printImage():
    
                    print('')
                    print('Image Found!')
    
                buttonImageExt = Button(main, command = printImage)
                buttonImageExt.pack()
                return buttonImageExt
    
            buttonImage = Button(main, command = selectOption_Image_Button)
            buttonImage.pack()
            return buttonImage
    
        def selectOption_Texture():
    
            def selectOption_Texture_Button():
                
                def printTexture():
    
                    print('')
                    print('Texture Found!')
    
                buttonTextureExt = Button(main, command = printTexture)
                buttonTextureExt.pack()
                return buttonTextureExt
    
            buttonTexture = Button(main, command = selectOption_Texture_Button)
            buttonTexture.pack()
            return buttonTexture
    
    
        def selectOption_Preview():
    
            def selectOption_Preview_Button():
    
                def printPreview():
    
                    print('')
                    print('Preview Render!')
    
                buttonPreviewExt = Button(main, command = printPreview)
                buttonPreviewExt.pack()
                return buttonPreviewExt
    
            buttonPreview = Button(main, command = selectOption_Preview_Button)
            buttonPreview.pack()
            return buttonPreview
    
        def selectOption_Export():
    
            def selectOption_Export_Button():
    
                def printExport():
    
                    print('')
                    print('Source Exported !')
    
                buttonExportExt = Button(main, command = printExport)
                buttonExportExt.pack()
                return buttonExportExt
    
            buttonExport = Button(main, command = selectOption_Export_Button)
            buttonExport.pack()
            return buttonExport
    
    optionlist = ('Image', 'Texture', 'Preview', 'Export')
    
    variable = StringVar()
    variable.set(optionlist[0])
    
    optionmenu = OptionMenu(main, variable, *optionlist, command = selectOption)
    optionmenu.pack()
    
    
    main.mainloop()
    This is an simple test to train my self in programming and write software... but if i run this script,
    i get an error... for more information...

    This my error (# Image Button):
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 3436, in __call__
    self.__callback(self.__value, *args)
    File "C:/Users/Gebruiker/Desktop/Options.py", line 14, in selectOption
    selectOption_Image()
    UnboundLocalError: local variable 'selectOption_Image' referenced before assignment
    This is my error (#Texture Button):
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 3436, in __call__
    self.__callback(self.__value, *args)
    File "C:/Users/Gebruiker/Desktop/Options.py", line 14, in selectOption
    selectOption_Image()
    UnboundLocalError: local variable 'selectOption_Image' referenced before assignment
    This my error (# Preview Button):
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 3436, in __call__
    self.__callback(self.__value, *args)
    File "C:/Users/Gebruiker/Desktop/Options.py", line 14, in selectOption
    selectOption_Image()
    UnboundLocalError: local variable 'selectOption_Image' referenced before assignment
    And the error in the Export Button:
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 3436, in __call__
    self.__callback(self.__value, *args)
    File "C:/Users/Gebruiker/Desktop/Options.py", line 14, in selectOption
    selectOption_Image()
    UnboundLocalError: local variable 'selectOption_Image' referenced before assignment
    Pleace... said not thad this program is boring... this only an simple test to figure out, how i can
    create diffrenct menu buttons... but i need some help with tkinter...

    Can you correct my code, just i can expiriment with it ?... thanks for help,
    Jamie...
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    I think this is a nesting problem. These local variables referenced before assignment take place because of incorrect indentation, unless you intend for these functions to be defined within the selectOption function, in which case the definitions need to precede their use.
    Code:
    from tkinter import *
    
    main = Tk()
    
    def selectOption(optionselect):
    
        global select
    
        select = optionselect
    
        select = 'Image'
    
        if select == 'Image':
            selectOption_Image()
            buttonTexture.config(text = 'Texture' % (buttonTexture, buttonTextureExt))
            buttonPreview.config(text = 'Preview' % (buttonPreview, buttonPreviewExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
        if select == 'Texture':
            selectOption_Texture()
            buttonImage.config(text = 'Image' % (buttonImage, buttonImageExt))
            buttonPreview.config(text = 'Preview' % (buttonPreview, buttonPreviewExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
        if select == 'Preview':
            selectOption_Preview()
            buttonImage.config(text = 'Image' % (buttonImage, buttonImageExt))
            buttonTexture.config(text = 'Texture' % (buttonTexture, buttonTextureExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
    
        if select == 'Export':
            selectOption_Export()
            buttonImage.config(text = 'Image' % (buttonImage, buttonImageExt))
            buttonTexture.config(text = 'Texture' % (buttonTexture, buttonTextureExt))
            buttonExport.config(text = 'Export' % (buttonExport, buttonExportExt))
    
    
    def selectOption_Image():
    
        def selectOption_Image_Button():
    
            def printImage():
    
                print('')
                print('Image Found!')
    
            buttonImageExt = Button(main, command = printImage)
            buttonImageExt.pack()
            return buttonImageExt
    
        buttonImage = Button(main, command = selectOption_Image_Button)
        buttonImage.pack()
        return buttonImage
    
    def selectOption_Texture():
    
        def selectOption_Texture_Button():
    
            def printTexture():
    
                print('')
                print('Texture Found!')
    
            buttonTextureExt = Button(main, command = printTexture)
            buttonTextureExt.pack()
            return buttonTextureExt
    
        buttonTexture = Button(main, command = selectOption_Texture_Button)
        buttonTexture.pack()
        return buttonTexture
    
    
    def selectOption_Preview():
    
        def selectOption_Preview_Button():
    
            def printPreview():
    
                print('')
                print('Preview Render!')
    
            buttonPreviewExt = Button(main, command = printPreview)
            buttonPreviewExt.pack()
            return buttonPreviewExt
    
        buttonPreview = Button(main, command = selectOption_Preview_Button)
        buttonPreview.pack()
        return buttonPreview
    
    def selectOption_Export():
    
        def selectOption_Export_Button():
    
            def printExport():
    
                print('')
                print('Source Exported !')
    
            buttonExportExt = Button(main, command = printExport)
            buttonExportExt.pack()
            return buttonExportExt
    
        buttonExport = Button(main, command = selectOption_Export_Button)
        buttonExport.pack()
        return buttonExport
    
    optionlist = ('Image', 'Texture', 'Preview', 'Export')
    
    variable = StringVar()
    variable.set(optionlist[0])
    
    optionmenu = OptionMenu(main, variable, *optionlist, command = selectOption)
    optionmenu.pack()
    
    
    main.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo