sonus21
sonus21

Reputation: 5388

Python : Overriding a method from a third-party module (Tkinter)?

In Tkinter we are having few functions from module tkSimpleDialog .

from Tkinter import  *
import tkSimpleDialog

def t():
    root = Tk()
    f = tkSimpleDialog.askinteger("Add New User","Enter card Number")
    print f;
    mainloop()
t()

Above code snippet prompt for input.I need to write my own function for error handling (when user click OK and doesn't enter any thing or invalid input ) and some other properties like resizing , height , width e.t.c. of dialog box .

How to override function e.g. ok ?

Upvotes: 1

Views: 386

Answers (1)

kartikg3
kartikg3

Reputation: 2620

You could make your own dialog class that inherits from tkSimpleDialog. This way you could override any of the class' core methods. Classic OOP and extension to the rescue.

Check this documentation out for a tutorial: http://effbot.org/tkinterbook/tkinter-dialog-windows.htm

Another one: http://effbot.org/tkinterbook/tkinter-entry-dialogs.htm

You could take a look at tkSimpleDialog.py source to see the available class methods and see which ones you can extend. Quicker way is to do dir(tkSimpleDialog) after you have imported it, to see all its methods, properties and classes.

Always remember, if something is a class, there exists an opportunity to extend it into your own!

All the best.

Upvotes: 2

Related Questions