添加另一个标签时如何删除1个标签 - python tkinter

时间:2017-01-02 15:43:48

标签: python tkinter optionmenu

python相当新。 我正在使用选项菜单,并附有标签:

from tkinter import *
root=Tk()

def f(s):
    if s=="btn":
        one=Label(root,text="one blah blah")
        one.grid(column=1,row=2)
    if s=="btn2":
        two=Label(root,text="two")
        two.grid(column=1,row=2)


v=StringVar(root)
v.set("f")
a=OptionMenu(root,v,"btn","btn2",command=f)
a.grid(column=1,row=1)

root.configure()
root.geometry("100x100")
root.mainloop()

我无法弄清楚如何使“一个等等”删除“两个”,以便在按btn1后按btn2时不可见。我试过.grid_forget和类似的东西,但我永远无法让它工作。

如果重要的话,这是一个大型程序的示例程序,我创建的地方有很多不同的选项菜单和标签。

2 个答案:

答案 0 :(得分:1)

您可以在开始时创建空标签,然后仅更改此标签中的文字

import tkinter as tk

# --- functions ---

def f(s):
    if s == "btn":
        l['text'] = "one blah blah"
    elif s == "btn2":
        l['text'] = "two"
    else:
        print('ERROR: unknow:', s)

# --- main ---

root = tk.Tk()
root.geometry("100x100")

v = tk.StringVar(value="f")

a = tk.OptionMenu(root, v, "btn", "btn2", command=f)
a.grid(column=1, row=1)

l = tk.Label(root)
l.grid(column=1, row=2)

root.mainloop()

如果您必须删除Label(因为您必须放置不同的小部件 - 即。Button),请使用destroy()

import tkinter as tk

# --- functions ---

def f(s):
    global w # inform function to use external variable when you will use `=`

    if s == "label":
        if w: # check if widget already exist
            w.destroy()

        w = tk.Label(root, text="Hello World!")
        w.grid(column=1, row=2)

    elif s == "button":
        if w: # check if widget already exist
            w.destroy()

        w = tk.Button(root, text="Click Me")
        w.grid(column=1, row=2)

    else:
        print('ERROR: unknow:', s)

# --- main ---

root = tk.Tk()
root.geometry("100x100")

v = tk.StringVar(value="f")

a = tk.OptionMenu(root, v, "label", "button", command=f)
a.grid(column=1, row=1)

w = None # create global variable without value
         # to use later with widget (and keep access to this widget)

root.mainloop()

BTW:您只能创建一次窗口小部件并替换它们 - 然后使用grid_forget()隐藏窗口小部件

import tkinter as tk

# --- functions ---

def f(s):
    global w # inform function to use external variable when you will use `=`

    if s == "label":
        if w: # check if widget already exist
            w.grid_forget()

        w = l
        w.grid(column=1, row=2)

    elif s == "button":
        if w: # check if widget already exist
            w.grid_forget()

        w = b
        w.grid(column=1, row=2)

    else:
        print('ERROR: unknow:', s)

# --- main ---

root = tk.Tk()
root.geometry("100x100")

v = tk.StringVar(value="f")

a = tk.OptionMenu(root, v, "label", "button", command=f)
a.grid(column=1, row=1)

# create but not display
l = tk.Label(root, text="Hello World!")
b = tk.Button(root, text="Click Me")

w = None # create global variable without value
         # to use later with widget (and keep access to this widget)

root.mainloop()

如果您检查s

的所有可能值,则功能可以缩短
def f(s):
    global w # inform function to use external variable when you will use `=`
    if w: # check if widget already exist
        w.grid_forget()

    if s == "label":
        w = l
    elif s == "button":
        w = b

    w.grid(column=1, row=2)

答案 1 :(得分:0)

尝试修改 one=Label(root,text="one blah blah",width=20)two=Label(root,text="two",width=20) 或放置适当的宽度。没关系。

代码如下:

from tkinter import *

root=Tk()

def f(s):

  if s=="btn":

    one=Label(root,text="one blah blah",width=20)

    one.grid(column=1,row=2)

  if s=="btn2":

    two=Label(root,text="two",width=20)

    two.grid(column=1,row=2)


v=StringVar(root)

v.set("f")

a=OptionMenu(root,v,"btn","btn2",command=f)

a.grid(column=1,row=1)

root.configure()

root.geometry("100x100")

root.mainloop()

一个等等等等将被完全擦除,而两个将会被写入。