打开一个新窗口并返回

时间:2019-08-30 11:13:32

标签: python-3.x tkinter

我用tkinter创建了几个单独的窗口,现在我想连接它们。 因此,实现了几个“前进”和“后退”按钮。这应该很简单,但事实证明,这确实让我很困惑。

这可以看作是此问题的实际应用问题: Go back and forth between tkinter windows

我尽我所能,但仍然失败了。

我的具体问题是:

  • 当我点击“报告”按钮时,将打开新窗口,但是: 原始窗口(座舱)没有消失,第二个窗口打开 这只是“原始驾驶舱”窗口,但其中没有任何小部件 它(只是框架?)

  • 当我按下“后退”按钮时,也会出现此错误消息: self.ReportSelection_Win.withdraw()AttributeError:“事件”对象 没有属性'ReportSelection_Win'

我剥离了以下大多数功能的代码,因为它们对于我认为的问题不是必需的。因此大多数按钮没有任何功能。

from tkinter import * 
from tkinter import ttk  


#Funktionen für Fenster    
def Func_Show_Rep(self):    
    # Open new Window
    ReportSelection_Win = Tk()
    self.ReportSelection_Win = Toplevel()  

    ReportSelection_Win.title("Report auswählen")

    #Dropdown Auswahlliste
    Reports = [
    "Alle Mitarbeiter",
    "Alle Projekte",
    "Alle Skills"
    ]

    #Widgets
    #Labels & Dropdown
    Lbl_Headline = Label(ReportSelection_Win, text = "Bitte wählen Sie einen Report")#Create Label
    Lbl_Headline.grid(column=0, row=0, padx=10, pady=10) #Show Label

    Drop_Reports = ttk.Combobox(ReportSelection_Win)
    Drop_Reports.grid(column=0, row=1, padx=10, pady=10)
    Drop_Reports['values'] = Reports

    #Buttons
    Btt_Confirm_RepSelect = Button(ReportSelection_Win, text="Auswählen")
    Btt_Confirm_RepSelect.bind("<Button-1>", Select_Report)
    Btt_Confirm_RepSelect.grid(column=0, row=2, padx=10, pady=10, sticky=W)

    Btt_Back_RepSelect = Button(ReportSelection_Win, text="Zurück")
    Btt_Back_RepSelect.bind("<Button-1>", Func_ReportSelection_Back)#Back to Cockpit
    Btt_Back_RepSelect.grid(column=0, row=2, padx=10, pady=10, sticky=E)

    self.Cockpit_Win.withdraw() #.deiconify() to show again

#Funktionen für Report Fenster    
def Func_ReportSelection_Back(self):
    self.ReportSelection_Win.withdraw()
    self.Cockpit_Win.deiconify()



#Modify the Window [◙Not essential for Issue]
Cockpit_Win.title("Ressourcen Verwaltung")
Cockpit_Win.columnconfigure(1, weight=1)

Lbl_Descr_MA = Label(Cockpit_Win, text = "Mitarbeiter verwalten und anlegen")#Create Label
Lbl_Descr_MA.grid(column=0, row=0, padx=10) #Show Label

Btt_Show_MA = Button(Cockpit_Win, text="Mitarbeiter", width=35)
Btt_Show_MA.bind("<Button-1>",Func_Show_MA)#Button click starts function
Btt_Show_MA.grid(column=1, row=0, padx=10, pady=7, sticky=E)

Lbl_Descr_Pro = Label(Cockpit_Win, text = "Projekte Verwalten und anlegen.")#Create Label
Lbl_Descr_Pro.grid(column=0, row=1, padx=10) #Show Label

Btt_Show_Pro = Button(Cockpit_Win, text="Projekte", width=35)
Btt_Show_Pro.bind("<Button-1>",Func_Show_Pro)#Button click starts function
Btt_Show_Pro.grid(column=1, row=1, padx=10, pady=7, sticky=E)

Lbl_Descr_Rep = Label(Cockpit_Win, text = "Report auswählen und ausgeben")#Create Label
Lbl_Descr_Rep.grid(column=0, row=2, padx=10) #Show Label

Btt_Show_Rep = Button(Cockpit_Win, text="Reports", width=35)
Btt_Show_Rep.bind("<Button-1>",Func_Show_Rep)#Button click starts function
Btt_Show_Rep.grid(column=1, row=2, padx=10, pady=7, sticky=E)

Btt_Cock_Abort = Button(Cockpit_Win, text="Abbrechen", width=35)
Btt_Cock_Abort.bind("<Button-1>",Func_Cock_Abort)#Button click starts function
Btt_Cock_Abort.grid(column=1, row=3, padx=10, pady=7, sticky=E)

Cockpit_Win.geometry('350x170') #Window size


#Make the windows stay (loop)      
Cockpit_Win.mainloop()

1 个答案:

答案 0 :(得分:1)

我将您的代码减少到几乎无用的情况。

from tkinter import * 
from tkinter import ttk  


#Funktionen für Fenster   
def Func_Show_Rep(even = None):
    global Cockpit_Win
    Cockpit_Win.withdraw()#.deiconify() to show again
    ReportSelection_Win = Toplevel() 
    Show_Rep(ReportSelection_Win, Cockpit_Win)

class Show_Rep():    
    # Hide old Window

    def __init__(self, master, Cockpit_Win):
        # Open new Window

        self.ReportSelection_Win =master 
        self.ReportSelection_Win.title("Report auswählen")

        #Dropdown Auswahlliste
        Reports = [
        "Alle Mitarbeiter",
        "Alle Projekte",
        "Alle Skills"
        ]

        #Widgets
        #Labels & Dropdown
        Lbl_Headline = Label(self.ReportSelection_Win, text = "Bitte wählen Sie einen Report")#Create Label
        Lbl_Headline.grid(column=0, row=0, padx=10, pady=10) #Show Label

        Drop_Reports = ttk.Combobox(self.ReportSelection_Win)
        Drop_Reports.grid(column=0, row=1, padx=10, pady=10)
        Drop_Reports['values'] = Reports

        #Buttons

        Btt_Back_RepSelect = Button(self.ReportSelection_Win, text="Zurück")
        Btt_Back_RepSelect.bind("<Button-1>", self.Func_ReportSelection_Back)#Back to Cockpit
        Btt_Back_RepSelect.grid(column=0, row=2, padx=10, pady=10, sticky=E)

    #Funktionen für Report Fenster    
    def Func_ReportSelection_Back(self, event = None):
        self.ReportSelection_Win.withdraw()
        Cockpit_Win.deiconify()


Cockpit_Win = Tk()
#Modify the Window [◙Not essential for Issue]
Cockpit_Win.title("Ressourcen Verwaltung")
Cockpit_Win.columnconfigure(1, weight=1)



Lbl_Descr_Rep = Label(Cockpit_Win, text = "Report auswählen und ausgeben")#Create Label
Lbl_Descr_Rep.grid(column=0, row=2, padx=10) #Show Label

Btt_Show_Rep = Button(Cockpit_Win, text="Reports", width=35)
Btt_Show_Rep.bind("<Button-1>",Func_Show_Rep)#Button click starts function
Btt_Show_Rep.grid(column=1, row=2, padx=10, pady=7, sticky=E)



Cockpit_Win.geometry('350x170') #Window size


#Make the windows stay (loop)      
Cockpit_Win.mainloop()

说明: 我创建了将您的报告写入窗口的类。 和隐藏main_window的函数,使Topwindow