In tkinter GUI, i have created a "signup to an account" window. If the user clicks the sign-up button with one or more entries (in entry boxes) unfilled, a error message box appears. The thing I want is that when the user clicks the "OK" button of tkinter error messagebox, the cursor should be moved to that particular empty entry box, hence indicating the user which entry box is empty.
from tkinter import *
import tkinter.messagebox
class MyGUI:
def __init__(self):
self.root = Tk()
self.entrybox = []
for i in range(9): #Creating 9 Entry Boxes
self.entrybox.append(Entry(self.root, width = 10, font = 'Verdana 10'))
for i in self.entrybox: #packing each Entry Box
i.pack(pady = 5)
okButton = Button(self.root, text = 'OK', command = self.get_entryvalues)
okButton.pack()
mainloop()
def get_entryvalues(self):
values = []
for i in self.entrybox:
values.append(i.get())
if '' in values:
tkinter.messagebox.showerror('Error', '1 or more entries are not filled!', parent = self.root)
else:
tkinter.messagebox.showinfo('Info', 'Form submitted', parent = self.root)
mygui = MyGUI()
Since you have not included any code, here is a snippet to help you through.
from tkinter import *
from tkinter import messagebox
root = Tk()
def something():
if e.get() == '':
messagebox.showerror('Error message box',
'Please fill the blank field', parent=root)
e.focus_force() #to get the lost focus back
l = Label(root, text='Snippet')
l.pack()
e = Entry(root)
e.pack(pady=10)
b = Button(root, text='Click me!!', command=something)
b.pack()
e.focus_force() # to have focus to the entry box wen the program starts
root.mainloop()
Here if you your entry field is blank it gives a messagebox and after you click on ok the focus goes back to the entry box of the window.
The parent
argument in messagebox
is totally optional if you have just 1 window, but it gives some focus to the window that the messagebox appears over (if you're working over with more than 1 window.
Let me know if anything is wrong or if you have doubts:D
You can just call focus()
on the first empty entry found in the for loop inside get_entryvalues()
function:
from tkinter import *
import tkinter.messagebox
class MyGUI:
def __init__(self):
self.root = Tk()
self.entrybox = []
for i in range(9): #Creating 9 Entry Boxes
btn = Entry(self.root, width=10, font='Verdana 10')
btn.pack(pady=5)
btn.bind('<Return>', self.next_entry)
self.entrybox.append(btn)
okButton = Button(self.root, text='OK', command=self.get_entryvalues)
okButton.pack()
self.root.mainloop()
def get_entryvalues(self):
for e in self.entrybox:
if e.get().strip() == '':
tkinter.messagebox.showerror('Error', '1 or more entries are not filled!', parent=self.root)
e.focus() # focus the empty entry
return
tkinter.messagebox.showinfo('Info', 'Form submitted', parent=self.root)
def next_entry(self, event):
event.widget.tk_focusNext().focus()
mygui = MyGUI()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.