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

    Join Date
    Oct 2017
    Posts
    42
    Rep Power
    1

    [Python 3 + Tkinter] HotKeys Events, Error Found!


    Hey Python Programmers...

    I try to figure out, how i can create an GUI with python 3 and tkinter....
    This is my script:

    Code:
    # GUITest
    from tkinter import *
    from tkinter import ttk
    from tkinter.filedialog import askopenfilename
    import winsound
    
    
    root = Tk()
    
    
    
    
    
    def __import(event):
        __input = event.askopenfilename(initialdir="C:/", filetypes=[("PNG Supported","*.png")], title = "Brouse: ")
    
        print(__input)
    
        try:
            with open(__input) as fileinput:
                print(fileinput)
    
                for file in fileinput:
                    listbox.insert(END, str(file))
                
        except:
            print('File not exit')
    
    
    def __playSounds(event):
    
        play = Tk()
    
        def __playSound_Exit():
            winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
    
        def __playSound_Excl():
            winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
    
        def __playSound_Aste():
            winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)
    
    
        playTitle = play.title("WinPlayer")
        playlabel_main = ttk.Label(event.play, text = "Player: ", foreground="white",background="black",font=("Arial", 18))
        playlabel_main.pack()
    
        play.geometry("100x200+250+250")
    
        buttonPlayExit = ttk.Button(event.play, text = "SystemExit", command=__playSound_Exit(event))
        buttonPlayExcl = ttk.Button(event.play, text = "SystemExclamation", command=__playSound_Excl(event))
        buttonPlayAste = ttk.Button(event.play, text = "SystemAsterisk", command=__playSound_Aste(event))
    
        buttonPlayExit.pack()
        buttonPlayExcl.pack()
        buttonPlayAste.pack()
    
        play.mainloop()
        
    def __exit(event):
        exit()
    
    
    Title = root.title("PNG2Text Converter")
    label_main = ttk.Label(root, text = "PNG2Text Converter: ", foreground="white",background="black",font=("Arial",18))
    label_main.pack()
    
    menu = Menu(root)
    root.config(menu=menu)
    root.geometry("250x150+300+300")
    
    label_files = ttk.Label(root, text = "Total: ", foreground="black",background="white",font=("Arial",12))
    label_files.pack(fill=X, padx=10, pady=10, side=LEFT)
    
    file = Menu(menu)
    file.add_command(label = 'Import', command=__import(event), accelerator = 'ctrl+i')
    file.add_command(label = 'Play', command=__playSounds(event), accelerator = 'ctrl+p')
    file.add_command(label = 'Exit', command=__exit(event), accelerator = 'ctrl+e')
    menu.add_cascade(label = 'File', menu = file)
    
    root.bind_all('<Control-Key-i>', __import)
    root.bind_all('<Control-Key-p>', __playSounds)
    root.bind_all('<Control-Key-e>', __exit)
    
    
    listbox = Listbox(root)
    listbox.pack(fill=BOTH, expand=1)
        
    
    event.root.mainloop()
    I try to make hotkeys for menu tools, but if i run this script... i get this error:

    Traceback (most recent call last):
    File "C:/Users/Gebruiker/Desktop/Studie/Python/Languale/GUI/GUI Project/PNG2Text.py", line 76, in <module>
    file.add_command(label = 'Import', command=__import(event), accelerator = 'ctrl+i')
    NameError: name 'event' is not defined
    Can anyone correct my code, so i can expiriment with it... this is an test to figure out how
    i can work with tkinter...

    Can anyone give my the just code ?... thanks for help,
    Jamie.
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,946
    Rep Power
    509
    Changing some of event to Event and then remembering that askopenfilename is not a method of event, the program runs somewhat. I don't have winsound and converted those to print statements as well.
    Code:
    # GUITest
    from tkinter import *
    from tkinter import ttk
    from tkinter.filedialog import askopenfilename
    #import winsound
    help(Event)
    root = Tk()
    
    def __import(event):
        __input = askopenfilename(initialdir="C:/", filetypes=[("PNG Supported","*.png")], title = "Brouse: ")
    
        print(__input)
    
        try:
            with open(__input) as fileinput:
                print(fileinput)
    
                for file in fileinput:
                    listbox.insert(END, str(file))
    
        except:
            print('File not exit')
    
    
    def __playSounds(event):
    
        play = Tk()
    
        def __playSound_Exit():
            print('winsound.PlaySound("SystemExit", winsound.SND_ALIAS)')
    
        def __playSound_Excl():
            print('winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)')
    
        def __playSound_Aste():
            print('winsound.PlaySound("SystemAsterisk", winsound.SND_ALIAS)')
    
    
        playTitle = play.title("WinPlayer")
        playlabel_main = ttk.Label(event.play, text = "Player: ", foreground="white",background="black",font=("Arial", 18))
        playlabel_main.pack()
    
        play.geometry("100x200+250+250")
    
        buttonPlayExit = ttk.Button(event.play, text = "SystemExit", command=__playSound_Exit(event))
        buttonPlayExcl = ttk.Button(event.play, text = "SystemExclamation", command=__playSound_Excl(event))
        buttonPlayAste = ttk.Button(event.play, text = "SystemAsterisk", command=__playSound_Aste(event))
    
        buttonPlayExit.pack()
        buttonPlayExcl.pack()
        buttonPlayAste.pack()
    
        play.mainloop()
    
    def __exit(event):
        exit()
    
    
    Title = root.title("PNG2Text Converter")
    label_main = ttk.Label(root, text = "PNG2Text Converter: ", foreground="white",background="black",font=("Arial",18))
    label_main.pack()
    
    menu = Menu(root)
    root.config(menu=menu)
    root.geometry("250x150+300+300")
    
    label_files = ttk.Label(root, text = "Total: ", foreground="black",background="white",font=("Arial",12))
    label_files.pack(fill=X, padx=10, pady=10, side=LEFT)
    
    file = Menu(menu)
    file.add_command(label = 'Import', command=__import(Event), accelerator = 'ctrl+i')
    file.add_command(label = 'Play', command=__playSounds(Event), accelerator = 'ctrl+p')
    file.add_command(label = 'Exit', command=__exit(Event), accelerator = 'ctrl+e')
    menu.add_cascade(label = 'File', menu = file)
    
    root.bind_all('<Control-Key-i>', __import)
    root.bind_all('<Control-Key-p>', __playSounds)
    root.bind_all('<Control-Key-e>', __exit)
    
    
    listbox = Listbox(root)
    listbox.pack(fill=BOTH, expand=1)
    
    
    Event.root.mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo