#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    USA
    Posts
    12
    Rep Power
    0

    I'm forgetting syntax - How do I subclass tkMessageBox?


    Code:
    import tkMessageBox
    
    class NowTranslating(tkMessageBox):
        def __init__(self, parent):
            tkMessageBox.__init__(self, parent)
    I'm wondering what I'm forgetting to do to the above code to make it run correctly. As of now, it gives me the Traceback error below:

    Traceback (most recent call last):
    File "C:/Current/MY_PYTHON/Py_OOP_progs/Test3.py", line 3, in <module>
    class NowTranslating(tkMessageBox):
    TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

    I'm still learning OO concepts, but normally I wouldn't even think of subclassing tkMessageBox. I'd just do:
    Code:
    nowTranslating = tkMessageBox.showinfo('Now translating hand histories...', 'Allow several seconds per file...')
    ...but when I use that line of code, I can't later do nowTranslating.iconify(), so I guess that I need to make a class that

    inherits from tkMessageBox so that I can get an instance name and then iconify() the message box window. I tried Googling for some

    example code that did this, but I couldn't find any so I guess that it's not a great way to do it, but I don't know. Any

    suggestions?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Following
    Code:
    import module
    the identifier "module" refers to the module "module" effectively from module.py. You cannot subclass a module. You can subclass other classes, such as tkMessageBox.Message


    What effect are you trying to achieve?
    Last edited by b49P23TIvg; November 20th, 2013 at 11:28 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    USA
    Posts
    12
    Rep Power
    0

    Re:


    Well, I have the code into two files, "SomeGUI.py" and "SomeMain.py". In the "SomeGUI.py", in the second to last line (commented), is where I have a message box appear. When the user navigates to this stage of the GUI and sees this message box, I am wanting to only give the option of waiting until file processing is done, then the prog will later display a report message box. I've never seen personally what the iconify method does, but as I was reading I thought that something like the following would do the trick:

    tkMessageBox.showinfo('Now translating hand histories...', 'Allow several seconds per file...').iconify()

    I might have a mistaken memory at the moment, but I'm pretty sure that I've tried that and several other variations. The last one I remember trying was something like:
    thisDialog = tkMessageBox.showinfo('...', '...').iconify()

    That didn't work, but from trying a variation of that and a print() statement let me know that its return type was a string, and so I needed to do the iconify() on an object, which lead me to trying to make a class that inherits from tkMessageBox: class NowTranslating(tkMessageBox):

    ...so that I can instantiate it and then run the iconify() method on it and see if it does what I need it to. Since I haven't yet seen any example code online of others subclassing tkMessageBox, I sort've thought that it wasn't allowable, or that I just didn't know how to reach its class, but my OOP knowledge and firsthand knowledge of Python libraries is small enough that I was out of ideas and just randomly trying things at that point.

    I probably don't understand correctly what your suggestion is. Should I maybe change my import statement to import tkMessageBox.Message and then I can subclass it? I'll have to pay more attention to the Python documentation for those things. I was unaware that tkMessageBox was just a module of class Message. It kind've makes sense though. Then, I'll be able to probably make a correct __init__ statement for the class NowTranslating. The one that's in my OP is just a result of me trying anything because I was baffled as to where I was going wrong.

    I should probably also mention that in my OP, the code that I listed is just from a small test.py file, and the traceback that is mentioned is not from my longer code below. I was just using the test.py code (above OP) to quickly make sure that I understood how to subclass a message box before I tried to adapt something that worked to my longer (below) code.


    SomeGUI.py
    Code:
    import sys
    import os
    from Tkinter import *
    import tkMessageBox
    
    tempString = "Click on the \"TRANSLATE\" button to begin.  Or click the \"X\" in the upper-right corner to close this program."
    
    class HhtStart_Window(Tk):
        def __init__(self, parent):
            Tk.__init__(self, parent)
            self.parent = parent
            self.initialize()
    
        def initialize(self):
            self.geometry("999x650+30+20")
    
            HhtswMessage = Message(self, text=tempString, width=979)
            HhtswMessage.pack(fill=X, side=TOP)
    
            CWD = os.getcwd()
            CWDlength = len(os.listdir(CWD))
            CWDFname = os.listdir(CWD)
    
            HhtswLabel1 = Label(self, text="The current working directory, from which this program instance is running, is:", padx=5, 
    
    pady=5,)
            HhtswLabel1.pack()
    
            HhtswLabel2 = Label(self, text=CWD, padx=5, pady=5, fg="blue")
            HhtswLabel2.pack()
    
            HhtswLabel3 = Label(self, text="The contents of this directory are:", padx=5, pady=5)
            HhtswLabel3.pack()
    
            HhtswScroll = Scrollbar(self)
            HhtswScroll.pack(side=RIGHT, fill=Y)
    
            HhtswListbox = Listbox(self, selectmode=SINGLE, fg="blue", width=68, height=5)
    
            xlsxCounter = 0
            for i in range(CWDlength):
                HhtswListbox.insert(END, CWDFname[i])
                if CWDFname[i].endswith('.xlsx') == True:
                    xlsxCounter = xlsxCounter + 1
    
            HhtswListbox.pack()
    
            HhtswScroll.config(command=HhtswListbox.yview)
            HhtswListbox.config(yscrollcommand=HhtswScroll.set)
    
            S30 = "The total number of items in this directory is " + str(CWDlength) + ".  Of these items, " + str(xlsxCounter) + " 
    
    is/are *.xlsx files."
            HhtswLabel4 = Label(self, text=S30, padx=5, pady=5)
            HhtswLabel4.pack()
    
            S31 = "Click on the \"TRANSLATE\" button to begin.  Or click the \"X\" in the upper-right corner to close this program."
            HhtswLabel5 = Label(self, text=S31, padx=5, pady=5)
            HhtswLabel5.pack()
    
            HhtswButton = Button(self, text="TRANSLATE", command = self.OnButtonClick)
            HhtswButton.pack()
    
            self.resizable(False, False)
    
        def OnButtonClick(self):
            result = tkMessageBox.askquestion("Do you need to CANCEL and EXIT? \n", "Click \"Yes\" to CANCEL and EXIT. \n Click \"No\" to 
    
    Translate hand histories")
            if result == 'yes':
                self.destroy()
            else:
                tkMessageBox.showinfo('Now translating hand histories...', 'Allow several seconds per file...')
                    ####^  In this line, I'm trying to do something like:  aMsgBx.showinfo('...', '...').iconify() ... ^####
                    #####  ...so when that dialog displays, it cannot disappear by the user clicking the 'OK' button.  #####
            return result
    SomeMain.py
    Code:
    from TestGUI import*
    
    if __name__ == "__main__":
        Hhtsw = HhtStart_Window(None)
        Hhtsw.title("Test HUSnG HH Translator")
    
    Hhtsw.mainloop()
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Maybe with more monkeys and more time you'll get it.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Location
    USA
    Posts
    12
    Rep Power
    0

    Re:


    "the identifier "module" refers to the module "module" effectively from module.py. You cannot subclass a module. You can subclass other classes, such as tkMessageBox.Message"

    Thanks for stating that rule to me. I was a bit burnt out before. Upon looking at it again I was thinking that there was something that I must've been missing (which there was), 'cause the above rule seemed too obvious for me to have overlooked. Then I looked at the source code for tkMessageBox.py, and seen its import statement of "from tkCommonDialog import Dialog". So, then I opened up tkCommonDialog.py and made sure there was a class Dialog in it. I then changed my import statement from "import tkMessageBox.Message" to "from tkMessageBox import Message" and it runs without errors. I can't believe I was missing something so obvious. There's a lesson I don't think I'll forget for a long time.

IMN logo majestic logo threadwatch logo seochat tools logo