简体   繁体   中英

Python TkInter Checkbutton not working

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM