【问题标题】:How to open a new window in python from another window?如何从另一个窗口在python中打开一个新窗口?
【发布时间】:2015-07-22 10:12:39
【问题描述】:

我对 tkinter 有疑问。当用户按下按钮时,我需要打开一个新窗口,其中包含一些参数数据。问题是当我创建新窗口时,条目的方法“设置”不起作用,但当我从主类创建窗口时它起作用。我能做些什么来解决这个问题? 这是按钮:

Button(self.frameTabella, text="prenota", command=lambda id=ConcreteLibro.getIdentificativo():self.prenotaLibro(id)).grid(row=i, column=14)

这是新窗口的代码:

class GUIPrestitoLibro:
    import datetime

    def __init__(self, id):
        self.GestioneLibri=GestioneLibri()
        self.finestraPrestito=Tk()
        self.finestraPrestito.geometry()
        self.finestraPrestito.title("Prestito/consultazione")
        today = datetime.today()

        idLibroLabel=Label(self.finestraPrestito, text="ID libro:").grid(row=1, column=0)
        self.idLibro=StringVar(value=id)
        idLibro=Entry(self.finestraPrestito, textvariable=self.idLibro, state="readonly").grid(row=1, column=1)

        idUtenteLabel=Label(self.finestraPrestito, text="ID utente:").grid(row=2, column=0)
        self.idUtente=StringVar()
        idUtente=Entry(self.finestraPrestito, textvariable=self.idUtente).grid(row=2, column=1)

        dataInizioPrestitoLabel=Label(self.finestraPrestito, text="Data inizio prestito:").grid(row=3, column=0)
        self.dataInizioPrestito=StringVar(value=today.strftime("%d/%m/%Y"))
        dataInizioPrestito=Entry(self.finestraPrestito, textvariable=self.dataInizioPrestito, state="readonly").grid(row=3, column=1)

        dataFinePrestitoLabel=Label(self.finestraPrestito, text="Data fine prestito:").grid(row=4, column=0)
        self.dataFinePrestito=StringVar()
        dataFinePrestito=Entry(self.finestraPrestito, textvariable=self.dataFinePrestito).grid(row=4, column=1)

        registra=Button(self.finestraPrestito, text="conferma", command=self.registraPrestito).grid(row=5, column=0)
        resetta=Button(self.finestraPrestito, text="resetta", command=self.resettaCampi).grid(row=5, column=1)

        self.finestraPrestito.mainloop()


    def resettaCampi(self):
        self.idUtente.set("")
        self.dataFinePrestito.set("")

    def registraPrestito(self):
        self.GestioneLibri.prestitoLibro(self.idLibro.get(), self.idUtente.get(), self.dataInizioPrestito.get(), self.dataFinePrestito.get())
        self.finestraPrestito.destroy()

如何使“GUIPrestitoLibro”类成为 Toplevel 的实例?

【问题讨论】:

    标签: python lambda tkinter window


    【解决方案1】:

    问题是这个类创建了Tk 的第二个实例。 tkinter 应用程序应该始终只有一个 Tk 实例,并且应该只调用一次 mainloop()

    如果你需要创建多个窗口,除了根窗口之外的所有窗口都必须是Toplevel的实例,并且你不应该在除根窗口之外的任何窗口上调用mainloop()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-09-24
      • 2012-12-12
      • 1970-01-01
      • 2020-03-19
      相关资源
      最近更新 更多