1

This is a piece of code I write for this question: Entry text on a different window?

It is really strange what happened at mySubmitButton, it appears that the button does not want to appear when it is first started, it will, however appear when you click on it. Even if you click on it and release it away from the button, that way it won't be send. I am suspecting if this only happen on a mac, or it only happen to my computer, because it is a very minor problem. Or it is something silly I did with my code.

self.mySubmitButton = tk.Button(top, text='Hello', command=self.send)
self.mySubmitButton.pack()

Am I missing something? I googled and found this question and answer on daniweb. And I do a diff on them, can't figure out what he did "fixed", but I did see the line is changed to command=root.quit. But it is different from mine anyway...

Here is the full source code, and there is no error message, but the button is just missing.

import tkinter as tk

class MyDialog:
    def __init__(self, parent):
        top = self.top = tk.Toplevel(parent)
        self.myLabel = tk.Label(top, text='Enter your username below')
        self.myLabel.pack()

        self.myEntryBox = tk.Entry(top)
        self.myEntryBox.pack()

        self.mySubmitButton = tk.Button(top, text='Hello', command=self.send)
        self.mySubmitButton.pack()

    def send(self):
        global username
        username = self.myEntryBox.get()
        self.top.destroy()

def onClick():
    inputDialog = MyDialog(root)
    root.wait_window(inputDialog.top)
    print('Username: ', username)

username = 'Empty'
root = tk.Tk()
mainLabel = tk.Label(root, text='Example for pop up input box')
mainLabel.pack()

mainButton = tk.Button(root, text='Click me', command=onClick)
mainButton.pack()

root.mainloop()

enter image description here

enter image description here

  1. Adding another button right after this one, the second one actually appear. I thought it might be because I didn't call the same function, but I called the same one and it does the exact same thing it appears...
  2. Adding a empty label between them, doesn't work. The button still isn't being draw.

enter image description here

PS: I am using Mac OS 10.5.8, and Tk 8.4.7.

Community
  • 1
  • 1
George
  • 4,514
  • 17
  • 54
  • 81
  • this works perfect in win7 64-bit with activepython 2.6.7 (after changing tkinter to Tkinter) either from the IDE (Stani's SPE) or started with double click. It could be then OS specific – joaquin Apr 07 '12 at 06:56
  • But the regular button on other program I wrote are fine, and even when I create an second one (with the same function), it appears, (just the middle one (the click me) one is missing)... It is not a big deal, but I am just curious and wonder. Or maybe my installation has some problem. – George Apr 07 '12 at 07:02
  • Your code works fine on my macintosh, with python 2.7. Maybe you have a buggy version of python/tkinter? – Bryan Oakley Apr 07 '12 at 11:30
  • @BryanOakley If I want to implement a pop up window, either a slide bar, input box, etc, radio button, is this the right way to do it? Any suggestion on the code itself? – George Apr 07 '12 at 14:17
  • @George: it's OK. but I would advise against using a global variable. Instead of having the dialog destroy itself, have it destroy only the actual widget but leave the object alive. Then, call something like `inputDialog.get_string()` and then `del inputDialog` from your main logic. – Bryan Oakley Apr 07 '12 at 15:20
  • @BryanOakley I thought the get and delete method should be implemented on the button (from the dialog), is there a way to tell the main logic to do the two operator after the click is being detected? And destroy the widget, do you mean the TopLevel? I am sincerely sorry to ask question in the comment, I don't know if it is good idea to open another question.... but I would like to learn how to implement this. (It is quite useful). – George Apr 07 '12 at 17:20
  • @George: comments are for clarification, not for extended discussions. If you have a question you want answered, ask it as a question. – Bryan Oakley Apr 07 '12 at 18:14

1 Answers1

4

I see the hello button, but I'm on windows 7.

I did a quick re-write of your example. I'll be curious if it makes any difference for you.

import tkinter as tk

class GUI(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)

        mainLabel = tk.Label(self, text='Example for pop up input box')
        mainLabel.pack()

        mainButton = tk.Button(self, text='Click me', command=self.on_click)
        mainButton.pack()

        top = self.top = tk.Toplevel(self)
        myLabel = tk.Label(top, text='Enter your username below')
        myLabel.pack()

        self.myEntryBox = tk.Entry(top)
        self.myEntryBox.pack()

        mySubmitButton = tk.Button(top, text='Hello', command=self.send)
        mySubmitButton.pack()

        top.withdraw()

    def send(self):
        self.username = self.myEntryBox.get()
        self.myEntryBox.delete(0, 'end')
        self.top.withdraw()
        print(self.username)

    def on_click(self):
        self.top.deiconify()

gui = GUI()
gui.mainloop()
Honest Abe
  • 8,430
  • 4
  • 49
  • 64
  • Your version worked like a charm, is it the way I write my `MyDialog()` class? The button appear without requiring me to "click" around or switch between tab. – George Apr 07 '12 at 07:12
  • I removed `self`, on my original code, it doesn't seems to work yet. But I suppose I am getting closer to the solution. Thanks @Honest Abe – George Apr 07 '12 at 07:15
  • @George It could be ;-]. Have you tried without the wait_window? – Honest Abe Apr 07 '12 at 07:18
  • @Abe Just tired, same result for now. I guess if anything I will just implement it in your structure, at least it works. Or switch to `wxpython` :D btw: don't know why @ Honest Abe doesn't work... And I also wonder if Linux experience this problem too, I will try next monday when I go back to the school's lab. – George Apr 07 '12 at 07:23
  • @George My guess is it has something to do with the fact that you are doing half of it in the main program and the other half in a class. I suggest doing it all one way or the other (like my example, or no class at all). Good night :-] – Honest Abe Apr 07 '12 at 07:31
  • @George I saw the question you were answering. He would want `self.username` to be able to use it in the next phase. Feel free to use any of this to answer his question. Cheers! – Honest Abe Apr 07 '12 at 07:46