0

(original question had incorrect code)

Rushy Panchal in 2013 gave a nice example of a small two-windows program (click "new window" --> new window appears --> click "quit" --> new window disappears...). I wonder why this does not work anymore, if the command used to call up the second window has an additional parameter. Then the two windows appear at the same time.

If I don't find out, I'll never be able to write a larger program.

In short:

def new_window(self): --> works

def new_window(self, i_text): --> doesn't work

There must be a not too complicated solution, but even after looking for two evenings, I just don't get it.

Here the original code:

import tkinter as tk

class Demo1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window)
        self.button1.pack()
        self.frame.pack()
    def new_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Demo2(self.newWindow)

class Demo2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
        self.quitButton.pack()
        self.frame.pack()
    def close_windows(self):
        self.master.destroy()

def main():
    root = tk.Tk()
    app = Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

This works great. Ok and now the modified version which does not work.

import Tkinter as tk

class Demo1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window('hello'))
        self.button1.pack()
        self.frame.pack()
    def new_window(self, i_text):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Demo2(self.newWindow)

class Demo2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
        self.quitButton.pack()
        self.frame.pack()
    def close_windows(self):
        self.master.destroy()

def main():
    root = tk.Tk()
    app = Demo1(root) 
    root.mainloop()

if __name__ == '__main__':
    main()
MartinM
  • 65
  • 1
  • 9
  • That's because you aren't changing what arguments you give to `Demo2`. The error will probably tell you that. When you call `Demo2(self.newwindow)`, you need to add the `itext` argument: `Demo2(self.newwindow, "mytext")`. – zondo Mar 02 '16 at 20:55
  • sorry I messed it up. I see the question as answered and put it in again with the "correct" code which shows the problem – MartinM Mar 02 '16 at 21:05
  • Problem is, you are calling `self.new_window` by adding paranthesis at the end. So second window appears because of `Toplevel` in `self.new_window`. To pass arguments to button, check the duplicate target. – Lafexlos Mar 02 '16 at 21:18
  • 1
    @Lafexlos, Bhargav: thanks so much, you saved my day (... ah ....) my night. That's exactly what I needed! – MartinM Mar 02 '16 at 21:45

0 Answers0