0

请帮我。这很奇怪。看这个:

#!/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()

似乎检查按钮变量仅在 mainFinestra 中设置。如果我在另一个新窗口(例如 insertWindow)中创建检查按钮,则 checkvars 中的变量始终为 0,即使这些按钮被选中。相反,如果我尝试检查 mainFinestra 中的检查按钮,则函数“show”如果被检查则返回 1。有什么不同?拜托,这个项目对我很重要。谢谢

4

1 回答 1

2

您正在做一些 Tkinter 不打算做的事情——您正在创建该类的两个实例Tk。您应该只创建一个实例,并且只启动一个事件循环。

如果您需要多个窗口,请创建Tkinter.Toplevel.

于 2012-09-26T13:56:17.873 回答