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

    Join Date
    Oct 2017
    Posts
    83
    Rep Power
    1

    [Python 3.x + Tkinter] Option Menu not Work, tclError Found!


    Hey GUI Programmers...

    I try to create an option menu thad can change the style, but i get an tclError...

    This is my script, writting in python 3.x...:
    Code:
    from tkinter import *
    
    
    def main():
        root = Tk()
        root.geometry('640x480')
        root.title('Styles')
        root.config(background = 'white')
    
        select = 'Defeult'
    
        def GetStyle(optionselect):
            global select
            select = optionselect
    
            if select == styles[0]:
                root.config(background = 'white')
                b.config(foreground = 'black', background = 'white')
                o.config(foreground = 'black', background = 'white')
    
            if select == styles[1]:
                root.config(background = 'gray')
                b.config(foreground = 'black', background = 'red')
                o.config(foreground = 'black', background = 'red')
    
            if select == styles[2]:
                root.config(background = 'black')
                b.config(foreground = 'white', background = 'purple')
                o.config(foreground = 'white', background = 'purple')
        
        b = Button(root, text = 'TestButton', foreground = 'black', background = 'white')
        b.pack(fill = X, side = TOP)
    
        styles = ['Defeult', 'Grayscale', 'Blackhole']
    
        ovar = StringVar()
        ovar.set(styles[0])
        o = OptionMenu(root, var = ovar, *styles, foreground = 'black', background = 'white', command = GetStyle)
    
    
        root.mainloop()
    
    if __name__ = '__main__':
        main()
    I don't know how i can fix this problem, but for more information to see whats wrong with my
    code, run this script and see my error pleace...

    Can anyone help me to fix this problem and correct my code, just i can experiment with it ?...
    Thanks, Jamie.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,968
    Rep Power
    509
    Run this.
    Code:
    import pprint
    
    from tkinter import *
    from tkinter import messagebox
    
    font=('fixed',40)
    
    
    def main():
        root = Tk()
        root.geometry('640x480')
        root.title('Styles')
        root.config(background = 'white')
    
        def option_selected(*args, **kwargs):
            print('args',args)
            print('kwargs',kwargs)
            messagebox.showinfo('OPTION CHOSEN---see console', ovar.get())
    
        select = 'Defeult'
    
        def GetStyle(optionselect):
            global select
            select = optionselect
    
            if select == styles[0]:
                root.config(background = 'white')
                b.config(foreground = 'black', background = 'white')
                o.config(foreground = 'black', background = 'white')
    
            if select == styles[1]:
                root.config(background = 'gray')
                b.config(foreground = 'black', background = 'red')
                o.config(foreground = 'black', background = 'red')
    
            if select == styles[2]:
                root.config(background = 'black')
                b.config(foreground = 'white', background = 'purple')
                o.config(foreground = 'white', background = 'purple')
    
        #print('dir(root)')
        #pprint.pprint(dir(root))
        b = Button(root, text = 'quit', foreground = 'black', background = 'white',font=('fixed',40), command = root.quit)
        b.pack(fill = X, side = TOP)
    
        styles = ['Read the manual', 'stop guessing', 'use online help','print(dir(obj))', 'use help(objct)']
    
        ovar = StringVar()
        ovar.set(styles[0])
        ovar.trace('w', option_selected)
        #pprint.pprint(dir(ovar))
    
        o = OptionMenu(root, ovar, *styles)
        o.configure(foreground = 'black', background = 'white' ,font=('fixed',40))
        o.pack()
    
        print('\n'*3)
        print('OptionMenu configuration does not include command')
        pprint.pprint(o.keys())
    
        #['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'cursor', 'direction', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'indicatoron', 'justify', 'menu', 'padx', 'pady', 'relief', 'compound', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']
    
        root.mainloop()
    
    if __name__ == '__main__':
        main()
    Last edited by b49P23TIvg; March 24th, 2018 at 12:42 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    83
    Rep Power
    1
    Originally Posted by b49P23TIvg
    Run this.
    Code:
    import pprint
    
    from tkinter import *
    from tkinter import messagebox
    
    font=('fixed',40)
    
    
    def main():
        root = Tk()
        root.geometry('640x480')
        root.title('Styles')
        root.config(background = 'white')
    
        def option_selected(*args, **kwargs):
            print('args',args)
            print('kwargs',kwargs)
            messagebox.showinfo('OPTION CHOSEN---see console', ovar.get())
    
        select = 'Defeult'
    
        def GetStyle(optionselect):
            global select
            select = optionselect
    
            if select == styles[0]:
                root.config(background = 'white')
                b.config(foreground = 'black', background = 'white')
                o.config(foreground = 'black', background = 'white')
    
            if select == styles[1]:
                root.config(background = 'gray')
                b.config(foreground = 'black', background = 'red')
                o.config(foreground = 'black', background = 'red')
    
            if select == styles[2]:
                root.config(background = 'black')
                b.config(foreground = 'white', background = 'purple')
                o.config(foreground = 'white', background = 'purple')
    
        #print('dir(root)')
        #pprint.pprint(dir(root))
        b = Button(root, text = 'quit', foreground = 'black', background = 'white',font=('fixed',40), command = root.quit)
        b.pack(fill = X, side = TOP)
    
        styles = ['Read the manual', 'stop guessing', 'use online help','print(dir(obj))', 'use help(objct)']
    
        ovar = StringVar()
        ovar.set(styles[0])
        ovar.trace('w', option_selected)
        #pprint.pprint(dir(ovar))
    
        o = OptionMenu(root, ovar, *styles)
        o.configure(foreground = 'black', background = 'white' ,font=('fixed',40))
        o.pack()
    
        print('\n'*3)
        print('OptionMenu configuration does not include command')
        pprint.pprint(o.keys())
    
        #['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'cursor', 'direction', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'indicatoron', 'justify', 'menu', 'padx', 'pady', 'relief', 'compound', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']
    
        root.mainloop()
    
    if __name__ == '__main__':
        main()


    OK, but your example is for my too dificult, i get an simple script thad don't works and pleace.... i need the simplest example to understand
    optionmenus.... so this is my script, writted in python 3:

    Code:
    from tkinter import *
    
    
    selected = 'One'
    
    
    master = Tk()
    
    def Options(select):
    
        global selected
        selected = select
    
        if selected == 'One':
            b.config(text = 'One1')
    
        if selected == 'Two':
            b.config(text = 'Two2')
    
        if selected == 'Three':
            b.config(text = 'Three3')
    
    Option_Names = ['One', 'Two', 'Three']
    w_var = StringVar(master)
    w_var.set(Option_Names[0])
    w = OptionMenu(master, w_var, *Option_Names, command = Options).pack(fill = X, side = TOP)
            
    b = Button(master, text = 'One1').pack(fill = X, side = TOP)
    
    master.mainloop()

    Pleace, can you give my the simplest script so i can understand this beter ?....., Thanks for help, Jamie.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Posts
    83
    Rep Power
    1
    Oj yeah.... This is my error i get with my simple script:
    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
    File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 3439, in __call__
    self.__callback(self.__value, *args)
    File "C:/Users/Gebruiker/Desktop/menubuttonTest.py", line 18, in Options
    b.config(text = 'Two2')
    AttributeError: 'NoneType' object has no attribute 'config'
    Thanks for help, Jamie.
  8. #5
  9. Lord of the Dance
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Oct 2003
    Posts
    4,160
    Rep Power
    2011
    You are calling pack directly on the button - and thereby get the return value from that function, which is NoneType (nothing):
    Code:
    b = Button(master, text = 'One1').pack(fill = X, side = TOP)
    If you want use 'b' as a button reference, you need to call pack in a separate call:
    Code:
    b = Button(master, text = 'One1')
    b.pack(fill = X, side = TOP)
    Last edited by MrFujin; April 28th, 2018 at 08:37 AM.

IMN logo majestic logo threadwatch logo seochat tools logo