hello daniweb community;

The first window my application shows is this one:

def init(win):
   win.title("Ausgangsposition")
   win.minsize(800, 600)
   getcurrentrateandcurrentbalance()
   btn.pack()

Usage of win:

win = Tk()
win = Toplevel()

methode getcurrentrateandcurrentbalance()

def getcurrentrateandcurrentbalance():
    threading.Timer(5.0, getcurrentrateandcurrentbalance).start()
    print "Hello, World!" + result
    lab=Label(win, text="Aktueller Kurs: " + res +" Euro/BTC", bg='#40E0D0', fg='#FF0000')
    lab2=Label(win,text="Aktuell verf\xfcgbare Coins: " + avbtcs +" BTCs", bg='#40E0D0', fg='#FF0000')
    lab.place(x=20, y=30)
    lab2.place(x=20, y=70)

After closing window "win" with this methode:

def hello():
   t = Toplevel(win)
   t.title("Schritt 2")
   t.configure(background='#40E0D0')
   win.destroy()

"win" is closed but cmd shows error logs every 5 seconds because of the label in "getcurrentrateandcurrentbalance()".
Any idea how to fix this? The methode seems to be called again although "win" is closed.

Kind regards;

Edited 2 Years Ago by j.heller

You have to destroy/quit the Tk() instance. You can not do that because "win" now points to a Toplevel. Give the Toplevel another, unique name.

win = Tk()
win = Toplevel()

Edited 2 Years Ago by woooee

How did you mean?

winds = Tk()
win = Toplevel()

  def hello():
   t = Toplevel(win)
   t.title("Schritt 2")
   t.configure(background='#40E0D0')
   win.destroy()

doesn't work.
Destroying the Tk() instance stops the whole application doesn't it?
It works with using:

def hello():
   t = Toplevel()
   t.title("Schritt 2")
   t.configure(background='#40E0D0')
   win.destroy()

winds = Tk()
win = Toplevel()

But the cmd triggers this error message:

Exception in thread Thread-4:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 1082, in run
    self.function(*self.args, **self.kwargs)
  File "GUI.py", line 32, in getcurrentrateandcurrentbalance
    lab=Label(win, text="A: " + res +" Euro/B", bg='#40E0D0', fg=
'#FF0000')
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2537, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2036, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
TclError: bad window path name ".33926160"

Here are the methodes used:

def init(win):
   win.title("Ausgangsposition")
   win.minsize(800, 600)
   getcurrentrateandcurrentbalance()
   btn.pack()

def getcurrentrateandcurrentbalance():
    threading.Timer(5.0, getcurrentrateandcurrentbalance).start()
    print "Hello, World!" + result
    lab=Label(win, text="A: " + res +" Euro/B", bg='#40E0D0', fg='#FF0000')
    lab2=Label(win,text="A: " + avbtcs +" BTCs", bg='#40E0D0', fg='#FF0000')
    lab.place(x=20, y=30)
    lab2.place(x=20, y=70) 

Edited 2 Years Ago by j.heller

The response was to your first error message.

win" is closed but cmd shows error logs every 5 seconds because of the label in "getcurrentrateandcurrentbalance()".
Any idea how to fix this? The methode seems to be called again although "win" is closed.

It was still running because Tk was still running. Your function calls itself repeatedly

def getcurrentrateandcurrentbalance():
    threading.Timer(5.0, getcurrentrateandcurrentbalance).start()

What is it you are trying to do? And why does creating a label require Threading and Timer? It is better to use Tkinter's after() and update() if necessary to call a funcion every x seconds. Click Here

Your next error will be because of an undeclared variable (at least in the code you posted).

print "Hello, World!" + result

"result" if not declared or updated in the function.

Edited 2 Years Ago by woooee

destroy() is used to destroy a particular window in a multiwindow program
you have to give all the Tk windows a unique name

Here I am again, I reduced the code a bit:

from Tkinter import *
import tkMessageBox

def init(win):
   win.title("Ausgangsposition")
   win.minsize(800, 600)
   win.configure(background='#40E0D0')
   win.resizable(width=FALSE, height=FALSE)
   lab=Label(win, text="Aktueller Kurs:  Euro/BTC", bg='#40E0D0', fg='#FF0000')
   lab2=Label(win,text="Aktuell verf\xfcgbare Coins: BTCs", bg='#40E0D0', fg='#FF0000')
   lab.place(x=20, y=30)
   lab2.place(x=20, y=70)

def messageWindow():
    windz = Toplevel()
    message = "This is the child window"
    Label(windz, text=message).pack()
    Button(windz, text='OK', command=root.destroy).pack()

root = Tk()
root.withdraw()
root.overrideredirect(1)
root.update()
root.deiconify()
btn = Button(root, text="Hello", command=messageWindow)
btn.pack()
init(root)
root.mainloop()

Click on button btn ("Hello") should open a new window, this works but the old window should be closed, so I can guide my user through steps of installation for example.

Keeping track of everything is going to become a pain without a class structure. Note the use of partial to pass the window id to the next function. You can do this but how are you going to keep track of the responses? I would suggest you consider a generic function that receives the questions or whatever and updates a dictionary with the answers. It then returns to a common window with a "Next" button, which destroys the previous window and calls the function again with the next question, etc.

from Tkinter import *
import tkMessageBox
from functools import partial

def init(win):
   win.title("Ausgangsposition")
   win.minsize(800, 600)
   win.configure(background='#40E0D0')
   win.resizable(width=FALSE, height=FALSE)
   lab=Label(win, text="Aktueller Kurs:  Euro/BTC", bg='#40E0D0', fg='#FF0000')
   lab2=Label(win,text="Aktuell verf\xfcgbare Coins: BTCs", bg='#40E0D0', fg='#FF0000')
   lab.place(x=20, y=30)
   lab2.place(x=20, y=70)

def messageWindow():
    root.withdraw()
    windz = Toplevel()
    message = "This is the child window"
    Label(windz, text=message).pack()
    Button(windz, text='Next', command=partial(next_message, windz)).pack()

def next_message(tk_id):
    tk_id.destroy()
    root. deiconify()
    Button(root, text='Quit', command=root.quit, bg="red").pack()

root = Tk()

## eliminate things not necessary for this example
#root.withdraw()
#root.overrideredirect(1)
#root.update()
#root.deiconify()

btn = Button(root, text="Hello", command=messageWindow)
btn.pack()
#init(root)
root.mainloop()
This article has been dead for over six months. Start a new discussion instead.