Reputation: 1
So, I've been trying to bind the enter key to press the button I have in this python program -- yes I've seen the numerous other questions related to this, but none of their code worked with this program for whatever reason. Posting code below to see if anyone has a good solution.
The below code does everything as expected -- it will pull up the GUI, show the goofy jar-jar picture, the button, and the entry fields with the prefilled text, but the enter key on my keyboard will not produce a result as it happens when I press the button with the mouse.
import tkinter as tk
from PIL import Image, ImageTk, ImageTransform
from tkinter.filedialog import askopenfile
root = tk.Tk()
class guiMenu:
def __init__(self, master):
myFrame = tk.Frame(master)
# logo
logo = Image.open('jar.jpg')
logo2 = logo.resize((200, 200), Image.ANTIALIAS)
logo2 = ImageTk.PhotoImage(logo2)
self.logo_label = tk.Label(image=logo2)
self.logo_label.image = logo2
self.logo_label.grid(column=1, row=0)
# instructions
self.instructions = tk.Label(master,
text="Input your email address and password to extract email attachments.\n "
"You should also select the folder you wish the attachments to reach.")
self.instructions.grid(columnspan=3, column=0, row=3)
# store the user login info in variables
def storeUserLogin():
clientEmailInput = self.emailEntry.get()
clientPasswordInput = self.passwordEntry.get()
print(clientEmailInput, clientPasswordInput)
# delete email prefill on click
def onEmailClick(event):
self.emailEntry.configure()
self.emailEntry.delete(0, 100) # this deletes the preexisting text for email entry
self.emailEntry.unbind('<Button-1>', self.on_click_id)
# delete pw prefill on click
def onPWClick(event):
self.passwordEntry.configure()
self.passwordEntry.delete(0, 100) # this deletes the preexisting text for email entry
self.passwordEntry.unbind('<Button-1>', self.on_click_id2)
# email entry box
self.emailEntry = tk.Entry(master, width=50)
# emailEntry = tk.StringVar(None)
# emailEntry.set("Email Address")
self.emailEntry = tk.Entry()
self.emailEntry.insert(0, "Email Address")
self.emailEntry.configure()
self.emailEntry.grid(column=1, row=1)
# on-click function
self.on_click_id = self.emailEntry.bind('<Button-1>', onEmailClick)
# enter key function
def enterFunction(event=None):
master.bind('<Return>', lambda event=None, loginButton.invoke())
# password entry box
self.passwordEntry = tk.Entry()
self.passwordEntry.insert(0, "Password")
self.passwordEntry.grid(column=1, row=2)
# on click function
self.on_click_id2 = self.passwordEntry.bind('<Button-1>', onPWClick)
# button to login
def loginButton():
self.loginButtonText = tk.StringVar()
self.loginButton = tk.Button(master, textvariable=self.loginButtonText, font="Arial",
commands=lambda: [storeUserLogin(), enterFunction()],
width=5, height=2,
bg="white", fg="black")
self.loginButtonText.set("LOGIN")
self.loginButton.grid(column=1, row=4)
self.canvas = tk.Canvas(root, width=600, height=250)
self.canvas.grid(columnspan=3)
g = guiMenu(root)
root.mainloop()
Upvotes: 0
Views: 661
Reputation: 21
You're Binding the loginButton function in another function which is never called and you are writing the OOPs wrong here. You shouldn't define your functions in init function.Here is some changes that i made in your code. I am not good at explaining but i hope this will help. I wrote reasons too in code where i change the code.
import tkinter as tk
from PIL import Image, ImageTk, ImageTransform
from tkinter.filedialog import askopenfile
root = tk.Tk()
class guiMenu:
def __init__(self, master):
#definr master in Class so we can use it from entire class
self.master = master
myFrame = tk.Frame(master)
# logo
logo = Image.open('Spitball\Spc.jpg')
logo2 = logo.resize((200, 200), Image.ANTIALIAS)
logo2 = ImageTk.PhotoImage(logo2)
self.logo_label = tk.Label(image=logo2)
self.logo_label.image = logo2
self.logo_label.grid(column=1, row=0)
#Create Email Entry box (we are creating Email and Password entry box when class is initialize)
self.emailEntry = tk.Entry(master, width=50)
self.emailEntry.insert(0, "Email Address")
# emailEntry = tk.StringVar(None)
# emailEntry.set("Email Address")
# self.emailEntry = tk.Entry() # You should not create two entry boxes with same name
# self.emailEntry.configure() # We dont need to configure email entry here
#Create Password Entry box (we are creating Email and Password entry box when class is initialize)
self.passwordEntry = tk.Entry()
self.passwordEntry.insert(0, "Password")
#Grid the email and password entry box.Here email entry box will display first beacuse we grid it first.
self.emailEntry.grid(column=1, row=1)
self.passwordEntry.grid(column=1, row=2)
# instructions
self.instructions = tk.Label(master,
text="Input your email address and password to extract email attachments.\n "
"You should also select the folder you wish the attachments to reach.")
self.instructions.grid(columnspan=3, column=0, row=3)
#Create Login Button
self.loginButtonText = tk.StringVar()
self.loginButton = tk.Button(self.master, textvariable=self.loginButtonText, font="Arial",
command=self.storeUserLogin,
width=5, height=2,
bg="white", fg="black")
# I dont see here the use of Canvas so i commented out it.
# self.canvas = tk.Canvas(self.master, width=600, height=250)
# on-click function
self.on_click_id = self.emailEntry.bind('', self.onEmailClick)
# on click function
self.on_click_id2 = self.passwordEntry.bind('', self.onPWClick)
#Bind enter key with loginButtonFunc
self.master.bind('',self.loginButtonFunc)
def storeUserLogin(self):
# store the user login info in variables
clientEmailInput = self.emailEntry.get()
clientPasswordInput = self.passwordEntry.get()
print(clientEmailInput, clientPasswordInput)
# delete email prefill on click
def onEmailClick(self,event):
self.emailEntry.configure()
self.emailEntry.delete(0, 100) # this deletes the preexisting text for email entry
self.emailEntry.unbind('', self.on_click_id)
# delete pw prefill on click
def onPWClick(self,event):
self.passwordEntry.configure()
self.passwordEntry.delete(0, 100) # this deletes the preexisting text for email entry
self.passwordEntry.unbind('', self.on_click_id2)
# button to login
def loginButtonFunc(self,event=None):
self.loginButtonText.set("LOGIN")
self.loginButton.grid(column=1, row=4,pady=5)
# self.canvas.grid(columnspan=3)
g = guiMenu(root)
root.mainloop()
Upvotes: 2