[英]How to clear a page on Tkinter
如何清除Tkinter上的窗口? 這是我的代碼:
import sys
from tkinter import *
mGui = Tk ()
mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = Label (text = "Welcome to MyMathDictionary. Press Next to continue.",fg = "blue",bg = "white").place (x= 150,y = 200)
mbutton = Button (text = "Next").place(x = 275,y = 230)
mGui.mbutton = (mbutton.forget())
您遇到的問題是由兩件事引起的: place
小部件放在define
小部件name = somewidget()
的同一行中,而實際上並沒有刪除小部件。 mGui.mbutton = (mbutton.forget())
實際上沒有做任何事情。 您應該在小部件定義行中使用command={function name}
,以便在單擊按鈕時可以調用一個函數。
.forget()
應該可以工作,但是使用錯誤。 您可能應該使用以下內容:
import sys
import tkinter
from tkinter import *
def next_screen():
mLabel1.place_forget()
mbutton.place_forget()
mGui = tkinter.Tk()
mGui.geometry("600x600+545+170")
mGui.title("MyMathDictionary")
mLabel1 = tkinter.Label(text="Welcome to MyMathDictionary. Press Next to continue.",
fg="blue", bg="white")
mLabel1.place(x=150, y=200)
mbutton = tkinter.Button(text="Next", command=next_screen)
mbutton.place(x=275, y=230)
將.pack()
或.place()
與按鈕或標簽的定義放在同一行中,將使小部件以某種方式變為無nonetype
。 我本人並不完全了解這一點,但是將widget.place()
放在單獨的行中會widget.place()
幫助,您可以自己對此進行測試。
更好的是像一個函數,它將小部件名稱列表作為輸入,並將其中的每個小部件都刪除:
mbutton = tkinter.Button(text="Next", command=forget_page1)
mbutton.place(x=275, y=230)
def next_screen(names):
for widget in names:
widget.place_forget()
def forget_page1():
widgets = [mLabel1, mbutton]
next_screen(widgets)
# Code for the creation of page2 widgets
# You could probably make a function for every page, but I'm sure
# someone could come up with a better answer, instead of repeat
# making functions.
def forget_page2():
widgets = [page2label, page2button, image]
next_screen(widgets)
# Code for the creation of the widgets on page3?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.