2016-09-25 57 views
0

这是一个小程序,当我从下拉菜单中选择一个选项时,我可以找到不同四边形的区域。但是,当我从假设广场梯形切换,我得到这个:Python 3.4 tkinter,从菜单中选择一个选项后没有清除框架

2

我要清除的窗口只剩下选定的选项。

下面是代码:

from tkinter import * 

def square(): 
    ment = IntVar() 
    def mhello(): 
     mtext = ment.get() 
     mtext *= 2 
     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Square").pack() 
    mbutton = Button(mGui, text= "Submit", command = mhello). pack() 

    nEntry = Entry(mGui, textvariable=ment).pack() 
def rectangle(): 
    oneMent = IntVar() 
    twoMent = IntVar() 
    def mhello(): 
     oneMtext = oneMent.get() 
     twoMtext = twoMent.get() 
     mtext = 0 
     mtext = oneMtext * twoMtext 
     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Rectangle/Parallelogram").pack() 

    mbutton = Button(mGui, text= "Submit", command = mhello). pack() 

    oneEntry = Entry(mGui, textvariable=oneMent).pack() 
    twoEntry = Entry(mGui, textvariable=twoMent).pack() 

def trapezium(): 
    oneMent = IntVar() 
    twoMent = IntVar() 
    threeMent = IntVar() 
    def mhello(): 
     oneMtext = oneMent.get() 
     twoMtext = twoMent.get() 
     threeMtext = threeMent.get() 
     mtext = 0 
     mtext = oneMtext + twoMtext 
     mtext /= 2 
     mtext *= threeMtext 

     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Trapezium").pack() 

    mbutton = Button(mGui, text= "Submit", command = mhello). pack() 

    oneEntry = Entry(mGui, textvariable=oneMent).pack() 
    twoEntry = Entry(mGui, textvariable=twoMent).pack() 
    threeEntry = Entry(mGui, textvariable=threeMent).pack() 

def rhombus(): 
    oneMent = IntVar() 
    twoMent = IntVar() 
    def mhello(): 
     oneMtext = oneMent.get() 
     twoMtext = twoMent.get() 
     mtext = 0 
     mtext = oneMtext * twoMtext 
     mtext /= 2 

     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Rhombus").pack() 

    mbutton = Button(mGui, text= "Submit", command = mhello). pack() 

    oneEntry = Entry(mGui, textvariable=oneMent).pack() 
    twoEntry = Entry(mGui, textvariable=twoMent).pack() 

def restart(): 
    mGui.destroy() 


mGui = Tk() 

mGui.geometry("450x450+500+300") 
mGui.title("Square Area Finder") 
mHomeLabel = Label(mGui, text="Use the drop down menu to select the quadrilateral you want to find the area of.").pack() 
menu = Menu(mGui) 
mGui.config(menu=menu) 
file =Menu(menu) 
file.add_command(label="Square", command=square) 
file.add_command(label="Rectangle/Parallelogram", command=rectangle) 
file.add_command(label="Trapezium", command=trapezium) 
file.add_command(label="Rhombus", command=rhombus) 
file.add_separator() 
file.add_command(label="Quit", command=restart) 
menu.add_cascade(label="Options", menu=file) 

mGui.mainloop() 

谢谢你给任何人,可以帮助。

+1

我没有看到任何地方,你甚至尝试清除窗口。 –

+0

@BryanOakley我已经添加到他的代码 –

回答

1

当从下拉菜单中选择不同的选项中选择你需要删除你有什么之前,然后创建新部件

在你的每一个功能,你做的形状,你需要首先摧毁窗口小部件,然后创建新的

我有固定的代码:

from tkinter import * 

global widgets # this list will contain widgets to be deleted 
widgets = [] 
def square(): 
    global widgets 
    for widget in widgets[:]: 
     widget.destroy() 
     widgets.remove(widget) 

    ment = IntVar() 
    def mhello(): 
     mtext = ment.get() 
     mtext *= 2 
     mlabel2 = Label(mGui, text=mtext) 

    mlabel = Label(mGui, text="Square") 
    mbutton = Button(mGui, text= "Submit", command = mhello) 

    nEntry = Entry(mGui, textvariable=ment) 
    widgets = widgets[:] + [mlabel, mbutton, nEntry] # destroy these later 

    for widget in widgets: 
     widget.pack() # pack them afterwards 

def rectangle(): 
    global widgets 
    for widget in widgets[:]: 
     widget.destroy() 
     widgets.remove(widget) 

    oneMent = IntVar() 
    twoMent = IntVar() 
    def mhello(): 
     oneMtext = oneMent.get() 
     twoMtext = twoMent.get() 
     mtext = 0 
     mtext = oneMtext * twoMtext 
     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Rectangle/Parallelogram") 

    mbutton = Button(mGui, text= "Submit", command = mhello) 

    oneEntry = Entry(mGui, textvariable=oneMent) 
    twoEntry = Entry(mGui, textvariable=twoMent) 
    widgets = widgets + [mlabel, mbutton, oneEntry, twoEntry] # destroy these later 
    for widget in widgets: 
     widget.pack() # pack them afterwards 

def trapezium(): 
    global widgets 
    for widget in widgets[:]: 
     widget.destroy() 
     widgets.remove(widget) 

    oneMent = IntVar() 
    twoMent = IntVar() 
    threeMent = IntVar() 
    def mhello(): 
     oneMtext = oneMent.get() 
     twoMtext = twoMent.get() 
     threeMtext = threeMent.get() 
     mtext = 0 
     mtext = oneMtext + twoMtext 
     mtext /= 2 
     mtext *= threeMtext 

     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Trapezium") 

    mbutton = Button(mGui, text= "Submit", command = mhello) 

    oneEntry = Entry(mGui, textvariable=oneMent) 
    twoEntry = Entry(mGui, textvariable=twoMent) 
    threeEntry = Entry(mGui, textvariable=threeMent) 
    widgets = widgets + [mlabel, mbutton, oneEntry, twoEntry, threeEntry] # destroy these later 
    for widget in widgets: 
     widget.pack() # pack them afterwards 

def rhombus(): 
    global widgets 
    for widget in widgets[:]: 
     widget.destroy() 
     widgets.remove(widget) 

    oneMent = IntVar() 
    twoMent = IntVar() 
    def mhello(): 
     oneMtext = oneMent.get() 
     twoMtext = twoMent.get() 
     mtext = 0 
     mtext = oneMtext * twoMtext 
     mtext /= 2 

     mlabel2 = Label(mGui, text=mtext).pack() 

    mlabel = Label(mGui, text="Rhombus") 

    mbutton = Button(mGui, text= "Submit", command = mhello) 

    oneEntry = Entry(mGui, textvariable=oneMent) 
    twoEntry = Entry(mGui, textvariable=twoMent) 
    widgets = widgets + [mlabel, mbutton, oneEntry, twoEntry] # destroy these later 
    for widget in widgets: 
     widget.pack() # pack them afterwards 

def restart(): 
    mGui.destroy() 


mGui = Tk() 

mGui.geometry("450x450+500+300") 
mGui.title("Square Area Finder") 
mHomeLabel = Label(mGui, text="Use the drop down menu to select the quadrilateral you want to find the area of.").pack() 
menu = Menu(mGui) 
mGui.config(menu=menu) 
file =Menu(menu) 
file.add_command(label="Square", command=square) 
file.add_command(label="Rectangle/Parallelogram", command=rectangle) 
file.add_command(label="Trapezium", command=trapezium) 
file.add_command(label="Rhombus", command=rhombus) 
file.add_separator() 
file.add_command(label="Quit", command=restart) 
menu.add_cascade(label="Options", menu=file) 

mGui.mainloop() 

此代码的作品,但它可能是短了很多,更高效

它很好的做法,把类似的代码放到一个单独的函数,而不是复制它的4倍

+0

如果这可以帮助你,我不介意一个很好的绿色勾号,如果您有任何问题,然后在我的文章上添加评论 –

+1

非常感谢,它真的帮助。 –

相关问题