首页 > 解决方案 > 如何从 Tkinter 中删除文本?

问题描述

我想知道你如何删除 Tkinter 中的文本。文字以红色圈出。

我的代码如下:

from tkinter import *
from tkinter.ttk import Combobox
import win32com.client

root = Tk()
root.title('PM1 Digital Checklist')
root.geometry("400x400")

def open_excel():
    if combo.get() == 'PPM8001':
        myLabel = Label(root, text="Prime Mover Number Selected is:").pack()
        myLabel = Label(root, text=combo.get()).pack()
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = True
        file = excel.Workbooks.Open(r"/path/to/PPM8001.xlsx")

    if combo.get() == 'PPM8002':
        myLabel = Label(root, text="Prime Mover Number Selected is:").pack()
        myLabel = Label(root, text=combo.get()).pack()
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = True
        file = excel.Workbooks.Open(r"/path/to/PPM8002.xlsx")

    if combo.get() == 'PPM8003':
        myLabel = Label(root, text="Prime Mover Number Selected is:").pack()
        myLabel = Label(root, text=combo.get()).pack()
        excel = win32com.client.Dispatch("Excel.Application")
        excel.Visible = True
        file = excel.Workbooks.Open(r"/path/to/PPM8003.xlsx")

options = ['PPM8001','PPM8002','PPM8003']

v = list(options)

combo = Combobox(root, values = v, width =40)
combo.set("Select which Prime Mover number")
combo.pack()

button = Button(root, text = "Select", command = open_excel).pack()

root.mainloop()

图片在这里:

在此处输入图像描述

标签: pythontkinter

解决方案


有两件事要解决:

  1. 利用
myLabel = Label(root, text="Prime Mover Number Selected is:")
myLabel.pack()

将 Label 实例实际放入变量中

  1. 利用
myLabel.destroy()

摆脱它。

希望这会有所帮助!


推荐阅读