0

Please, help me. This is very strange. Look at this:

#!/usr/bin/env python
from Tkinter import *
import database

def insertBook():
 insertWindow = Tk()
 insertWindow.title("Inserisci un nuovo romanzo nel database")

 checkvars = []
 checkvars.append(IntVar())
 checkvars.append(IntVar())

 Checkbutton(insertWindow, text = 'male', variable=checkvars[0]).pack()
 Checkbutton(insertWindow, text = 'female', variable=checkvars[1]).pack()
 Button(insertWindow, text= 'show', command=lambda: show(checkvars)).pack()


 insertWindow.mainloop()

def show(checkvars):
 print checkvars[0].get()
 print checkvars[1].get()

class AppBase:
def __init__(self, parent):

    self.quadro1 = Frame(parent)
    self.quadro1.pack()
    self.welcolmeLabel = Label(self.quadro1, text = "Benvenuto nel database dei romanzi di Lory")
    self.welcolmeLabel.pack()

    self.insertButton = Button(self.quadro1, command = insertBook);
    self.insertButton["borderwidth"] = 1
    self.insertButton["text"] = "Inserisci un libro nel database"
    self.insertButton["background"] = "pink"
    self.insertButton.pack(side = "left")

    self.quadro2 = Frame(parent)
    self.quadro2.pack()

    self.searchButton = Button(self.quadro1);
    self.searchButton["borderwidth"] = 1
    self.searchButton["text"] = "Ricerca nel database"
    self.searchButton["background"] = "blue"
    self.searchButton.pack(side = "left")

    self.showButton = Button(self.quadro1);
    self.showButton["borderwidth"] = 1
    self.showButton["text"] = "Mostra i generi di libro"
    self.showButton["background"] = "violet"
    self.showButton.pack(side = "left")

    self.exitButton = Button(self.quadro2, text = "Uscita", borderwidth = 1, background = "red", command = self.quadro1.quit)
    self.exitButton.pack(side = RIGHT, pady = 20)


if __name__ == '__main__':

 mainFinestra = Tk()
 mainFinestra.title('Database di Romanzi')
 app = AppBase(mainFinestra)


 listvars = []
 listvars.append(IntVar())
 listvars.append(IntVar())

 Checkbutton(mainFinestra, text = 'male', variable=listvars[0]).pack()
 Checkbutton(mainFinestra, text = 'female', variable=listvars[1]).pack()
 Button(mainFinestra, text= 'show', command=lambda: show(listvars)).pack()

 mainFinestra.mainloop()

It seems that checkbuttons variables are set only in the mainFinestra. If I create checkbuttons in another new window (such as insertWindow), the variables in checkvars are always 0, even if the buttons are checked. Instead if I try to check the checkbutton in the mainFinestra, the function "show" returns 1 if they are checked. What's the difference? Please, this project is important to me. Thanks

3
  • The indentation of your code is wrong. Commented Sep 26, 2012 at 13:54
  • You really shouldn't have mutiple instances of Tk() running around. I'm not even sure if the behavior of your gui is defined once you create a second Tk(). Commented Sep 26, 2012 at 13:55
  • Thank you very much.I have tried TopLevel and it seems to work. Very cool! Thank you!!!!!!!!!!! Commented Sep 26, 2012 at 14:02

1 Answer 1

2

You're doing something that Tkinter isn't designed to do -- you're creating two instances of the class Tk. You should only ever create one instance, and only ever start one event loop.

If you need multiple windows, create instances of Tkinter.Toplevel.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah ok...I understand. But If I want to open another window when click on a button...how Shall I do? I thought that Tk was used to open windows...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.