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

    Join Date
    Oct 2017
    Posts
    85
    Rep Power
    1

    [Python 3 + Tkinter] Diffrence Style Colors, Error Found!


    Hey Python Programmers...

    I try to create an menu thad can change the color of my title...

    This is my code (Tkinter, Python 3):
    Code:
    from tkinter import *
    from tkinter import ttk
    from tkinter.filedialog import askopenfilename
    
    main = Tk()
    main.styles['red', 'green', 'yellow', 'blue', 'black']
    
    def Open():
        name = askopenfilename(initialdir = 'C:/', filetypes = [('AVI Files', '*.avi')], title = "Open File")
    
        print(name)
    
        try:
            with open(name) as filename:
    
                listbox.insert(END, str(filename.name))
                print(filename.name)
    
        except:
            print("Session Failed !")
    
    
    def Exit():
        print("Exit...")
        exit()
    
    
    Title = main.title('AVI File Loader')
    label = ttk.Label(main, text = 'Load Movies', background = defeult_style)
    label.defeult_style = styles[0]
    label.pack()
    
    menu = Menu(main)
    main.config(menu=menu)
    main.geometry("150x250+300+300")
    
    file_label = Label(main, text = 'Files')
    file_label.pack()
    
    file = Menu(menu)
    file.add_command(label = 'Open', command = Open)
    file.add_command(label = 'Exit', command = Exit)
    menu.add_cascade(label = 'File', menu = file)
    
    style = Menu(menu)
    style.add_command(label = 'Red', command = label.styles[0])
    style.add_command(label = 'Green', command = label.styles[1])
    style.add_command(label = 'Yellow', command = label.styles[2])
    style.add_command(label = 'Blue', command = label.styles[3])
    style.add_command(label = 'Black', command = label.styles[4])
    menu.add_cascade(label = 'Styles', menu = style)
    
    listbox = Listbox(main)
    listbox.pack(fill = BOTH, expand = 1)
    
    
    main.mainloop()
    I want expiriment with user preference like tools... thad can chance things like the style
    of buttons, menu's, widget, etc... this is my first code thad must make my program
    changeble... bit if i run my script, i get this error:

    Traceback (most recent call last):
    File "C:/Users/Gebruiker/Desktop/AVILoader.py", line 6, in <module>
    main.styles['red', 'green', 'yellow', 'blue', 'black']
    File "C:\Users\Gebruiker\Desktop\Software\Python\Ware\lib\tkinter\__init__.py", line 2095, in __getattr__
    return getattr(self.tk, attr)
    AttributeError: '_tkinter.tkapp' object has no attribute 'styles'
    This is what i means...

    Can anyone correct my code please, so 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,972
    Rep Power
    510
    ttk has the Style class.

    >>> from tkinter import ttk
    >>> dir(ttk)
    >>> help(ttk.Style)
    [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
    85
    Rep Power
    1
    Originally Posted by b49P23TIvg
    ttk has the Style class.

    >>> from tkinter import ttk
    >>> dir(ttk)
    >>> help(ttk.Style)
    OK... can you give my an example code, pleace ?...
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,972
    Rep Power
    510
    Learning ttk hasn't made it onto my priority list. To access ttk.Style you'd need to use
    ttk.Style
    whereas you've tried to access the style attribute of a Tk object. (a tkinter.Tk object, which is confusing because you've the tkinter names directly into the module namespace with the *). And there wasn't one, hence the error.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo