这是我用来查找不同四边形区域的一个小程序,当我从下拉菜单中选择一个选项时,它可以工作。 但是当我从让方形转换为梯形时,我得到了这个:
我想清除窗口,只留下所选的选项。
这是代码:
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()感谢任何可以提供帮助的人。
This is a little program I made to find the area of different quadrilaterals, when I select a option from the drop down menu it works. But when I switch from let's say square to trapezium, I get this:
I want to clear the window leaving only the selected option.
Here is the code:
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()Thank you to anyone that can help.
最满意答案
当选择下拉菜单中的其他选项时,您需要删除之前的内容,然后创建新的小部件
在您创建形状的每个函数中,您需要首先销毁小部件,然后创建新小部件
我已修复代码:
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次是一种很好的做法
When a different option from the drop down menu is selected you need to delete what you had before, then create the new widgets
In each of your functions where you make a shape, you need to first destroy the widgets, then create the new ones
I have fixed the code:
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()This code works but it could be a lot shorter and more efficient
Its good practice to put similar code into one separate function instead of copying it 4 times
更多推荐
发布评论