-1

I want to launch a warning using tkMessageBox in Python3. This warning is supposed to launch when a user doesn't select an element from a listbox. Unfortunately whenever I try to implement message box it does not launch like it is supposed to. I have code for a script called pietalkgui.py which contains the code where I want to implement the message box:

from tkinter import messagebox

# Gives warning if no user is selected for whisper
def whisperwarning(self):
    # show warning to user
    showwarning("Select User","Select a user to whisper to!")

# Handles whisper
def whispermessage(self):
    # stores element selected in temp variable
    temp = self.userslist.get(self.userslist.curselection())
     # if no item is selected from userslist (listbox)
    if temp == "":
        # launch warning to user if no item is selected
        self.whisperwarning()
    else:
        # retrieves usernames from userslist
        username = temp
        # storing whisper
        outwhisper = ' /w "' + username +'" ' + self.messagebox.get("0.0",END)
        # handling whisper
        self.handler(outwhisper)
        # erase message in message box
        self.messagebox.delete("0.0",END)

Am I doing something wrong in the implementation of tkMessageBox? Or am I not properly checking if not item is selected from the listbox?

Archit Mahto
  • 29
  • 1
  • 4
  • Can you provide full code of your class, or a dummy version, that we could actually copy, paste and execute? – Marcin Dec 15 '14 at 05:55
  • Since my code is a project, I can't send the entire code over. So, I will send a link to the code in github. This is the following GitHub link: [My Project](https://github.com/Sqash/COMP3203-project/tree/archit-working) **In the branch archit-working**. It is in the src/ptgui directory. – Archit Mahto Dec 15 '14 at 06:03
  • Sorry, if I couldn't post the entire code of my class because there are functions from other classes needed for it to run. – Archit Mahto Dec 15 '14 at 06:18
  • I believe `showwarning` is a method of `messagebox`, so you should call it as `messagebox.showwarning(...)`. The fact that you didn't include the error you are getting makes it a bit harder to be sure about this though, but if the error is anything like `NameError: global name 'showwarning' is not defined`, this is probably it. – fhdrsdg Dec 15 '14 at 08:20
  • Have you actually initialised the `tkinter` package? You say "it does not launch," but that is not a very revealing error report. Are you actually creating an app and calling its `mainloop()" function? – holdenweb Dec 15 '14 at 09:44
  • Do you get errors? What does "does not launch" mean? – Bryan Oakley Dec 15 '14 at 12:11
  • When it "does not launch", the message box does not show a warning when it is supposed to. – Archit Mahto Dec 15 '14 at 15:55
  • So, you get no errors, and you've verified that the code is actually running? – Bryan Oakley Dec 15 '14 at 16:27
  • The code is running but there is a bug. When you click a whisper button the program gives an error in the terminal. The error is when I don't select an element in a listbox and it tries to run. – Archit Mahto Dec 15 '14 at 16:31
  • if you want help when you get errors, _you need to show us the error_. "does not launch" is not the same as "I get an error", and "I get an error" is not nearly as useful as showing the actual error message. – Bryan Oakley Dec 15 '14 at 16:31
  • That was my mistake. I will make a point to show the error for the next time I have a question. Sorry, if I didn't give as much information as I should have. – Archit Mahto Dec 15 '14 at 16:33

1 Answers1

0

It appears that you are calling the method showwarning, but haven't defined it or imported it. That is the name of a function the messagebox module, so perhaps you need to change this:

showwarning("Select User","Select a user to whisper to!")

... to this:

messagebox.showwarning("Select User","Select a user to whisper to!")

Also, FWIW, this code is slightly incorrect: self.messagebox.delete("0.0",END) -- text indices start at "1.0", not "0.0".

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • '0.0' will work *in this particular situation*, because tk converts it to '1.0'. However, it will so the same with any index before '1.0', such as '0.end'. So, '0.end', for instance, will never work. – Terry Jan Reedy Dec 15 '14 at 19:44