如何在tkinter python中刷新或销毁在画布中创建的框架?

时间:2019-02-10 14:38:01

标签: python-3.x tkinter tkinter-canvas

我有一个GUI,我在其中创建了一个画布,并在其中创建了两个框架并使用 canvas.create_window 。一个位于顶部,带有四个按钮。单击按钮后,创建一个框架,其中包含一些小部件,直到第一个框架。因此,现在我希望在切换按钮之间销毁(或刷新,清除或删除)上一帧。现在这些帧彼此堆叠在一起。下面是我的代码,

from tkinter import *
#Creating root window
root = Tk()
root.geometry("1000x1000")
#Still don't know how it works but some how made it work :P :P
root.grid_rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=0)
#Creaed canvas for setting up a scroll bar
canv=Canvas(root,width=850, height=650, background='blue')
vsb = Scrollbar(root, orient="vertical", command=canv.yview)
canv.config(yscrollcommand=vsb.set)
vsb.grid(row=1,column=1,sticky="ns")
canv.config(scrollregion=(0, 0,600,1000))
canv.grid(row=1, column=0)
canv.bind('<Configure>',canv.config(scrollregion=canv.bbox('all')))
#Frame for the top push buttons
mainframe1 = Frame(root, height = 20,width= 250,bg = 'white')
canvas_frame = canv.create_window((4, 4),window=mainframe1,anchor="nw")
b1 = Button(mainframe1,text = "butt 1",width = 24, height = 1,command=lambda: buttonClick(1))
b2 = Button(mainframe1,text = "butt 2",width = 24, height = 1,command=lambda: buttonClick(2))
b3 = Button(mainframe1,text = "butt 3",width = 24, height = 1,command=lambda: buttonClick(3))
b4 = Button(mainframe1,text = "Butt 4",width = 24, height = 1,command=lambda: buttonClick(4))
b1.grid(row=0, column=0, padx=15, pady=10)
b2.grid(row=0, column=1, padx=15, pady=10)
b3.grid(row=0, column=2, padx=15, pady=10)
b4.grid(row=0, column=3, padx=15, pady=10)
#Method to work based on Button click
def buttonClick(num):
    if num == 1:
        #Creating Frame inside Canvas
        mainframe2 = Frame(root, height = 200,width= 250,bg = 'white')
        canvas_frame = canv.create_window((80,100),window = mainframe2,anchor="nw")
        label = Label(mainframe2,text="frame for butt 1",font=('Courier',-20,'bold'))
#label.grid_rowconfigure(1,weight=1)
#label.columnconfigure(1,weight=1)
        label.grid(row=0,column=3)
        #Create a Tkinter variable
        varmonth = StringVar()
        varyear = StringVar()
        varmonth.set('January')  # set the default option
        varyear.set('2017')
       #Option Menu for Dropdown list boxes
        #For Month
        popupMenu = OptionMenu(mainframe2, varmonth, 'January', 'February', 'March', 'April', 'May','June','July','August','Septempber','October','November','December')
        Label(mainframe2, text="Select Month",font=('Courier',-15,'bold')).grid(row=2, column=1,sticky='e',padx=15,pady=15)
        popupMenu.grid(row=2, column=2,padx=20,sticky="ew")
        #For year
        popupMenu = OptionMenu(mainframe2, varyear, '2017','2018','2019')
        Label(mainframe2, text="Select Year",font=('Courier',-15,'bold')).grid(row=2, column=7,padx=20,pady=25)
        popupMenu.grid(row=2, column=8,padx=15,pady=20)
        #For Generate Report Button
        buttongreport = Button(mainframe2,text = 'Generate Sales report',width = 24, height = 1,command=lambda: buttonClick(gr))
        buttongreport.grid(row=8, column=3,columnspan=2, padx=10, pady=10)

    elif(num==2):
        mainframe3 = Frame(root, height=200, width=250, bg='white')
        canvas_frame = canv.create_window((80, 100), window=mainframe3, anchor="nw")
        label = Label(mainframe3, text="Frame for butt 2", font=('Courier', -20, 'bold'))
        # label.grid_rowconfigure(1,weight=1)
        # label.columnconfigure(1,weight=1)
        label.grid(row=0, column=3)
        # Create a Tkinter variable
        varmonth = StringVar()
        varyear = StringVar()
        varmonth.set('January')  # set the default option
        varyear.set('2017')

        popupMenu = OptionMenu(mainframe3, varmonth, 'January', 'February', 'March', 'April', 'May', 'June', 'July',
                               'August', 'Septempber', 'October', 'November', 'December')
        Label(mainframe3, text="Select Month", font=('Courier', -15, 'bold')).grid(row=2, column=1, sticky='e', padx=15,
                                                                                   pady=15)
        popupMenu.grid(row=2, column=2, padx=20, sticky="ew")

        popupMenu = OptionMenu(mainframe3, varyear, '2017', '2018', '2019')
        Label(mainframe3, text="Select Year", font=('Courier', -15, 'bold')).grid(row=2, column=7, padx=20, pady=25)
        popupMenu.grid(row=2, column=8, padx=15, pady=20)

        buttongreport = Button(mainframe3, text='Generate Sales report', width=24, height=1,
                               command=lambda: buttonClick(gr))
        buttongreport.grid(row=8, column=3, columnspan=2, padx=10, pady=10)
    elif(num==3):
        mainframe4 = Frame(root, height=200, width=250, bg='white')
        canvas_frame = canv.create_window((80, 100), window=mainframe4, anchor="nw")
        label = Label(mainframe4, text=" Frame for Butt 4", font=('Courier', -20, 'bold'))
        # label.grid_rowconfigure(1,weight=1)
        # label.columnconfigure(1,weight=1)
        label.grid(row=0, column=2)
        # Create a Tkinter variable
        varmonth = StringVar()
        varyear = StringVar()
        varmonth.set('1: January to April')  # set the default option
        varyear.set('2017')

        popupMenu = OptionMenu(mainframe4, varmonth, '1: January to April', '2: May to August', '3: Septempber to December')
        Label(mainframe4, text="Select Quarter", font=('Courier', -15, 'bold')).grid(row=2, column=1, sticky='e', padx=15,
                                                                                   pady=15)
        popupMenu.grid(row=2, column=2, padx=20, sticky="ew")

        popupMenu = OptionMenu(mainframe4, varyear, '2017', '2018', '2019')
        Label(mainframe4, text="Select Year", font=('Courier', -15, 'bold')).grid(row=2, column=7, padx=20, pady=25)
        popupMenu.grid(row=2, column=8, padx=15, pady=20)

        buttongreport = Button(mainframe4, text='Generate Sales report', width=24, height=1,
                               command=lambda: buttonClick(gr))
        buttongreport.grid(row=8, column=2, padx=10, pady=10)

root.mainloop()

我已经以程序方式编写了代码。抱歉,冗长的代码。

2 个答案:

答案 0 :(得分:0)

单击按钮时,可以使用canvas.delete(...)删除当前显示的框架。但是您需要声明一个全局变量来保存当前显示的帧:

open_frame = None

然后修改buttonClick(...)

def buttonClick(num):
    global open_frame
    if open_frame:
        canv.delete(open_frame)
    if num == 1:
        ...
        open_frame = canv.create_window(...)
        ...
    elif num == 2:
        ...
        open_frame = canv.create_window(...)
        ...
    elif num == 3:
        ...
        open_frame = canv.create_window(...)
        ...

答案 1 :(得分:0)

您可以使用frame.destroy()

destroy()方法将删除框架,并且其所有子项也将被破坏。