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

    Join Date
    Oct 2017
    Posts
    46
    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,951
    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!

IMN logo majestic logo threadwatch logo seochat tools logo