Is there a way to pass arguments to a tkinter entry validation function?

I’m starting with python and I wanted to create a rock, paper, scissors game using tkinter.
So I created an entry to prompt the user for their name/username, and using some code I found I made so that the entry doesn’t allow to use spaces or to write a name longer than 12 characters.
I also made an advice label that tells the user when they encounter this limitations.

The problem is that I want to use this function more times, so i want to place the advise label in some other entries that are in other positions, and the best way to do so is to use arguments. But I can’t figure out how to pass them throught the validation function.

So this is some recreation code that actually works, because the arguments values were manually placed where they should be:

import tkinter as tk

root = tk.Tk()

class Main(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.pack(expand= True, fill='both')
        self.columnconfigure((0,1), weight= 1)
        self.rowconfigure((0,1,2,3),weight= 1)

        # I dont understand how this works
        self.vcmd= (self.register(self.validate), '%P')

        # Creation and grid setup of the entry
        self.entry_name= tk.Entry(self, text= '', validate= 'key', validatecommand=self.vcmd)
        self.entry_name.grid(row= 3, column= 0, sticky= 'nesw')

    #I don't know how can I give the entry and parent parmeters
    def validate(self, P, entry= tk.Entry, parent= tk.Frame): 

        # I should get the grid info from the entry
        info= self.entry_name.grid_info() 
        advise_text= tk.StringVar()
        advise_label= tk.Label(self,textvariable= advise_text) #I set the error label in the parent frame

        if ' ' in P:
            #if there are spaces, reject it
            advise_text.set('Spaces not allowed')
            advise_label.grid(row= info['row'], column= info['column'], sticky= 'e') #I place the error message in the same grid position as the entry
            advise_label.after(1000, lambda: advise_label.destroy())
            return False
        
        if len(P) <= 12:
            # Entry with shorter than 12 characters is ok
            return True
        else:
           # Anything else, reject it
            advise_text.set(f'12 characters max')
            advise_label.grid(row= info['row'], column= info['column'], sticky= 'e') #I place the error message in the same grid position as the entry
            advise_label.after(1000, lambda: advise_label.destroy())
            return False



root.geometry('600x600')
main=Main(root)
root.mainloop()

And this is how i want it to work but i don’t know how to pass the arguments from the validatecommand:

import tkinter as tk

root = tk.Tk()

class Main(tk.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        self.pack(expand= True, fill='both')
        self.columnconfigure((0,1), weight= 1)
        self.rowconfigure((0,1,2,3),weight= 1)

        # I dont understand how this works
        self.vcmd= (self.register(self.validate), '%P')

        # Creation and grid setup of the entry
        self.entry_name= tk.Entry(self, text= '', validate= 'key', validatecommand=self.vcmd)
        self.entry_name.grid(row= 3, column= 0, sticky= 'nesw')

    #I don't know how can I give the entry and parent parmeters
    def validate(self, P, entry= tk.Entry, parent= tk.Frame): 

        # I should get the grid info from the entry
        info= entry.grid_info() 
        advise_text= tk.StringVar()
        advise_label= tk.Label(parent,textvariable= advise_text) #I set the error label in the parent frame

        if ' ' in P:
            #if there are spaces, reject it
            advise_text.set('Spaces not allowed')
            advise_label.grid(row= info['row'], column= info['column'], sticky= 'e') #I place the error message in the same grid position as the entry
            advise_label.after(1000, lambda: advise_label.destroy())
            return False
        
        if len(P) <= 12:
            # Entry with shorter than 12 characters is ok
            return True
        else:
           # Anything else, reject it
            advise_text.set(f'12 characters max')
            advise_label.grid(row= info['row'], column= info['column'], sticky= 'e') #I place the error message in the same grid position as the entry
            advise_label.after(1000, lambda: advise_label.destroy())
            return False



root.geometry('600x600')
main=Main(root)
root.mainloop()

I apreciate if you can help me understand how does the register and validatecommand works, and therefor solve this problem.

New contributor

Pedro Villarrubia Betancort is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật