Got this code from somewhere online. I looked at the other SO answer but it didn't work for me. What should I fix this error
Question: How to fix "- _tkinter.TclError: no events specified in binding"
import tkinter as tk
fields = ['Email', 'Password', 'School']
def fetch(entries):
for entry in entries:
field = entry[0]
text = entry[1].get()
print('%s: "%s"' % (field, text))
def makeform(root, fields):
entries = []
for field in fields:
row = tk.Frame(root)
lab = tk.Label(row, width=15, text=field, anchor='w')
ent = tk.Entry(row)
row.pack(side=tk.TOP, fill=tk.X, padx=5, pady=5)
lab.pack(side=tk.LEFT)
ent.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.X)
entries.append((field, ent))
return entries
def getCreds():
root = tk.Tk()
ents = makeform(root, fields)
root.bind('', (lambda events=ents: fetch(e)))
b1 = tk.Button(root, text='Show',
command=(lambda events=ents: fetch(e)))
b1.pack(side=tk.LEFT, padx=5, pady=5)
b2 = tk.Button(root, text='Quit', command=root.quit)
b2.pack(side=tk.LEFT, padx=5, pady=5)
root.mainloop()
getCreds()
_tkinter.TclError: no events specified in binding
This should be relatively obvious. If you look at any of the many many examples of bind() on SO or google you will find that the first argument always has something specific in it and never an empty string.
Take some time to read up on tkinter-events-and-bindings.
There are 2 problems with you root.bind() 1st any time you click anywhere on the screen it will call the function. This is likely not what you want.
The 2nd problem both with your bind and the button command is your lambda. events=ents: fetch(e) you define your list of entries as events but then pass e to the function. So you have to correct that.
Personally I would create the list in the same place you define the root as well as define the root in the global name space. This will allow us to avoid the lambda as well.
import tkinter as tk
fields = ['Email', 'Password', 'School']
def fetch(_=None):
for ndex, entry in enumerate(entries):
print('{}: {}'.format(fields[ndex], entry.get()))
root = tk.Tk()
root.config(background='gray')
entries = []
for ndex, field in enumerate(fields):
tk.Label(root, width=15, text=field, anchor='w').grid(row=ndex, column=0, sticky='ew')
entries.append(tk.Entry(root))
entries[-1].grid(row=ndex, column=1, sticky='ew')
# The problem with root.bind is that it will constantly be calling the function anywhere you click on root.
root.bind('<Button-1>', fetch)
tk.Button(root, text='Show', command=fetch).grid(row=len(fields)+1, column=1, sticky='ew')
tk.Button(root, text='Quit', command=root.quit).grid(row=len(fields)+2, column=1, sticky='ew')
root.mainloop()
Related
Really hoping to get an explanation /fix for this issue. Using Python3 and tkinter.
I have a program window that calls a chain of functions for log in to a database. One of them calls a toplevel() log in dialog that should return an array / list of values. The toplevel dialog creates a set of entries that are read by a nested function and values stashed in a global list and then destroys toplevel window. The list is returned by the outer function to an original calling function.
Works great except if this function call originates from a higher level TK dialog. As is, I have to close out the entire program to get a return from that function. How do I fix this? Function listed here
Thanks!
# GUI constructor for log in
def login():
# Create logwin window
logwin = Toplevel()
logwin.title("Log In")
# Called by login() GUI/bsubmit - returns credentials from log in GUI
def userinfo():
global carray
sname=serventry.get()
lname=lnentry.get()
pname=passentry.get()
carray = [sname, lname, pname]
logwin.destroy()
print("userinfo() = ", carray)
# Create GUI
servlabel = Label(logwin, text="Server Address: ")
servlabel.grid(row=0, column=0)
serventry = Entry(logwin)
serventry.grid(row=0, column=1)
serventry.focus()
lnlable = Label(logwin, text="User Name: ")
lnlable.grid(row=1, column=0, padx=5)
lnentry = Entry(logwin)
lnentry.grid(row=1, column=1, padx=4)
passlabel = Label(logwin, text="Enter Password: ")
passlabel.grid(row=2, column=0)
passentry = Entry(logwin, show="*")
passentry.grid(row=2, column=1)
bsubmit = Button(logwin, text="Submit", command=userinfo, default='active')
bsubmit.grid(row=4, column=1, columnspan=2)
logwin.mainloop()
print("login() end:", carray)
return(carray)
I found the answer to this one:
I was using logwin.destroy in the inner function userinfo() and for whatever reason this did not allow the function to continue while the main Tk() construct was running. I then tried placing logwin.quit() on the inner loop and this allowed the function to progress as intended but did not destroy the logwin construct. Placing logwin.destroy() after the logwin.mainloop() then takes care of this. Data is passed back to my program and life is good.
def login():
# Create logwin window
logwin = Toplevel(root)
logwin.title("Log In")
# Called by login() GUI/bsubmit - returns credentials from log in GUI
def userinfo():
global carray
sname=serventry.get()
lname=lnentry.get()
pname=passentry.get()
carray = [sname, lname, pname]
logwin.quit()
print("userinfo() = ", carray)
return carray
# Create GUI
servlabel = Label(logwin, text="Server Address: ")
servlabel.grid(row=0, column=0)
serventry = Entry(logwin)
serventry.grid(row=0, column=1)
serventry.focus()
lnlable = Label(logwin, text="User Name: ")
lnlable.grid(row=1, column=0, padx=5)
lnentry = Entry(logwin)
lnentry.grid(row=1, column=1, padx=4)
passlabel = Label(logwin, text="Enter Password: ")
passlabel.grid(row=2, column=0)
passentry = Entry(logwin, show="*")
passentry.grid(row=2, column=1)
bsubmit = Button(logwin, text="Submit", command=userinfo, default='active')
bsubmit.grid(row=4, column=1, columnspan=2)
logwin.bind('<Return>', userinfoalt) # binds alternate credentials function to the return key.
logwin.mainloop()
logwin.destroy()
print("login() end:", carray)
return(carray)
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I have a small issue for which I can`t find the reason.
I have the following GUI, and it pops the message box when I run it, even though it is inside a procedure which is triggered only on button press.
Tried even creating a secondary function which will show only the message box and still did not fix the issue.
Thank you for your help... I am quite sure that there is an easy fix which I just do not see...
import tkinter as tk
from tkinter import ttk
import tkinter.messagebox
import jl_generator
def run():
jl_generator.run_process()
tkinter.messagebox.showerror('Done','Done')
def show():
temp_list = user_input_list
for i in range(0, len(user_input_list[0])):
listBox.insert("", "end", values = (user_input_list[0][i],user_input_list[1][i],user_input_list[2][i],user_input_list[3][i],user_input_list[4][i],user_input_list[6][i],user_input_list[8][i]))
# Column Names for the TreeView
cols = ('Entity', 'Customer Nr', 'Account Code', 'Profit Centre', 'Partner Profit Centre', 'Amount', 'Nr Of Journal Lines')
# Input data for the tree view
user_input_list, journal_code = jl_generator.get_user_input()
#Creating the
root = tk.Tk()
root.title('JL Generator')
#Create the treeview
listBox = ttk.Treeview(root, columns=cols, show='headings')
for col in cols:
listBox.heading(col, text=col)
listBox.grid(row=1, column=0, columnspan=3)
#-------------LABELS--------------
#Title Label
label = tk.Label(root, text="Journal Lines Generator", font=("Arial",30)).grid(row=0, columnspan=3)
#Journal Code Label
show_journal_code = tk.Label(root, text = 'Journal Code = ' + journal_code).grid(row=6, column=1)
#Number of Journal Lines Label
show_number_of_journal_lines = tk.Label(root, text = 'Number of Journal Lines = ' + str(sum(user_input_list[8][i] for i in range(0, len(user_input_list[0]))))).grid(row=5, column=1)
#------------BUTTONS-----------
#Run the Generation
run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)
#Show the input data
showScores = tk.Button(root, text="Show Input", width=15, command=show).grid(row=4, column=0)
#Close the window
closeButton = tk.Button(root, text="Exit", width=15, command=exit).grid(row=4, column=2)
root.mainloop()
run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)
this is incorrect.
I used to feel confused about this.
You should use:
run_process = tk.Button(root, text="Generate JLs", width=15, command=run).grid(row=4, column=1)
In python,function is an object,call function should use function()
If you debug this code,you will find that after debug this code
run_process = tk.Button(root, text="Generate JLs", width=15, command=run()).grid(row=4, column=1)
you will find it will call run function and run it.
And Finally,run_process["command"] will be the returned value ofrun()
I would like to use Tkinter to be able to disable one entry if 'no' is selected from a drop down menu.
from tkinter import *
def disableEntry(entry):
entry.config(state='disable')
def allowEntry(entry):
entry.config(state='normal')
def main():
print("test")
root = Tk() #create a TK root window
root.title("Lunch and Learn") #Title of the window
L1 = Label(root, text = "Label 1").grid(row=0, column=0, padx=30, pady=(20,5))
L2 = Label(root, text = "Label 2").grid(row=1, column=0, pady=5)
var = StringVar()
E1 = Entry(root,bd =3)
E1.grid(row=0, column=1)
D1 = OptionMenu(root,var,"yes","no")
D1.grid(row=1,column=1)
if var.get() == 'no':
disableEntry(E1)
elif var.get() == 'yes':
allowEntry(E1)
B2 = Button(text = "Submit", command=main).grid(row=4, column=2)
root.mainloop()
the above code is a simple example of what i have tried. I have created two functions called 'disableEntry' and 'allowEntry' which should change the state of the entry box but they don't appear to do anything when i change the input of the drop down menu.
i dont know if i am approaching this the wrong way or if there is a standardized way to do this.
any help would be appreciated.
You need a way to check the state of the selection after it is changed. That can be achieved with adding a callback command to the OptionMenu widget.
You were checking the correct variable, but the point you were checking it at was before the screen window had even displayed.
from tkinter import Label, StringVar, OptionMenu, Entry, Tk, Button
# change the state of the Entry widget
def change_state(state='normal'):
E1.config(state=state)
def main():
print("test")
# callback function triggered by selecting from OptionMenu widget
def callback(*args):
if var.get() == 'no':
change_state(state='disable')
elif var.get() == 'yes':
change_state(state='normal')
root = Tk() #create a TK root window
root.title("Lunch and Learn") #Title of the window
L1 = Label(root, text="Label 1").grid(row=0, column=0, padx=30, pady=(20, 5))
L2 = Label(root, text="Label 2").grid(row=1, column=0, pady=5)
var = StringVar()
E1 = Entry(root, bd=3)
E1.grid(row=0, column=1)
D1 = OptionMenu(root, var, "yes", "no", command=callback)
D1.grid(row=1, column=1)
B2 = Button(text = "Submit", command=main).grid(row=4, column=2)
root.mainloop()
Im just simply trying to print the values of the entry fields and it tells me its not defined. I been looking online for a long while now. The lastest thing that I tried was adding self as a parameter of add_student but that didn't work.
This is the code
from tkinter import *
class window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.widgets()
def add_student(self):
print(f"Student's name (first, last): {self.fname_textbox}, {self.lname_textbox}")
def widgets(self):
self.master.title("Student Test Score & Grade")
self.master.minsize(200,200)
""" Labels """
# First name
self.fname_label = Label(root, text='First Name: ')
self.fname_label.grid(row=0, column=0, padx=5, pady=5)
# Last Name
self.lname_label = Label(root, text='Last name: ')
self.lname_label.grid(row=1, column=0, padx=5, pady=5)
""" Entry boxes """
# First Name
self.fname_textbox = Entry(root, width=30)
self.fname_textbox.grid(row=0, column=1, padx=5)
# Last name
self.lname_textbox = Entry(root, width=30)
self.lname_textbox.grid(row=1, column=1, padx=5)
""" Buttons """
# Add Button
self.add_btn = Button(root, text="Add Student", command=self.add_student).grid(row=4, column=2, padx=2, pady=2)
if __name__=="__main__":
root = Tk()
root.resizable(width=False, height=False)
app = window(root)
root.mainloop()
It prints this
Student's name (first, last): .!entry, .!entry2
Instead of: Student's name (first, last): John, Doe
What does this mean?
You need to get the value of the entries in order to use them in your print statement
You do this by putting:
firstname = self.fname_textbox.get()
lastname = self.1fname_textbox.get()
before you print and using these values in your print statement.
Ah, Try something like this:
def __init__(self, master):
self.nameEntry = Entry(master)
self.contents = StringVar()
self.nameEntry["textvariable"]=self.contents
self.nameEntry.pack()
self.nameEntry.grid(row=0,column=1)
self.buttonSave = Button(master,text="Save",command=self.save).grid(row=9,columnspan=2, sticky=W+E+N+S)
def save(self):
print self.nameEntry.get()
im new to Python3.6.2
So I want a program that takes an input (Via text box from tkinter) and outputs a word in my custom "language"
with this function
def Mescre(n):
Words = (n)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
print(Words.translate(Mes))
and here's what i want the window to look like
from tkinter import*
root = Tk()
Mescre = Label(root, text="Input:")
English = Label(root , text="Output:")
label1.grid(row=0, sticky=E)
label2.grid(row=1, sticky=E)
entry1 = Entry(root)
entry2 = Entry(root)
entry1.grid = (row=0, column=1)
entry2.grid = (row=1, column=1)
root.mainloop()
if "hello" was in the Input text box, i want the output to be "coqqu" in the Output text box.
See my example below:
from tkinter import *
class App:
def __init__(self, root):
self.root = root
self.sv = StringVar()
self.Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
self.entry = Entry(self.root, textvariable = self.sv)
self.label = Label(self.root)
self.entry.pack()
self.label.pack()
self.sv.trace("w", self.callback)
def callback(self, *args):
self.label.configure({"text": self.entry.get().translate(self.Mes)})
root = Tk()
App(root)
root.mainloop()
Here we define a StringVar() to be the value of the attribute textvariable for the Entry widget.
We then assign a callback to a trace() on the variable so that whenever the variable is updated (When someone types in the Entry) we call callback().
Within callback() we use configure() on the Label widget in order to set the text to equal the post translation version of the value of the Entry widget.
This creates a "live updating" translation effect.
Here's a basic example:
import tkinter as tk
root = tk.Tk()
def Mescre():
val = textfield.get()
Words = (val)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
print(Words.translate(Mes))
textfield = tk.Entry(root)
textfield.pack()
button = tk.Button(root, command=Mescre, text='Push')
button.pack()
root.mainloop()
Updated:
import tkinter as tk
root = tk.Tk()
def Mescre():
val = textfield.get()
Words = (val)
Mes = str.maketrans('abcdefghijklmnopqrstuvwxyz', 'ektnopzcamjqwyuxsbfdiglhrv')
translation = Words.translate(Mes)
#print(translation)
outputfield.delete(0, tk.END)
outputfield.insert(0, translation)
textfield = tk.Entry(root)
textfield.pack()
outputfield = tk.Entry(root)
outputfield.pack()
button = tk.Button(root, command=Mescre, text='Push')
button.pack()
root.mainloop()