【问题标题】:How do I delete existing text in Tkinter?如何删除 Tkinter 中的现有文本?
【发布时间】:2019-10-24 10:00:23
【问题描述】:

我正在尝试使用 if 语句创建一个更改动画演示文稿中幻灯片的功能,但是当幻灯片应该更改时,现有的小部件会保留。这是我的最小可复制示例(顺便说一下,“description.insert”在我的计算机上的格式正确,但在我的手机上却没有):

from tkinter import *
import random
import time
tk = Tk()
canvas = Canvas(tk, width = 400, height = 400)
tk.title('Diffusion')
canvas.pack()
 
slide = 0
 
def changeSlide():
    global slide
    slide += 1
    #WRITE TEXT
    if slide == 1:
        description = Text(tk, bd=0, height=5, width = 50)
        description.insert(INSERT, 'Diffusion is the net movement of particles from an area of higher concentration to an area of lower concentration, which results in the concentration being even. Here is an example, with the particles represented by orange dots:')
        description.place(x=0, y=190)
    elif slide == 2:
        #DRAW PARTICLES  
        particle = canvas.create_oval(10, 10, 20, 20, fill = 'orange')
 
nexT = Button(tk, text = 'NEXT', command = changeSlide)
nexT.pack()
nexT.place(bordermode = 'inside', x = 350, y = 375)

【问题讨论】:

  • “现有的小部件保持不变”:在创建新小部件之前,您必须.destroy() 现有的小部件。
  • @stovfl 它适用于文本,但是当我尝试摧毁它
  • @stovfl 没关系...我解决了。不过感谢您的帮助!

标签: python function if-statement button tkinter


【解决方案1】:

虽然听起来您可能已经找到了解决问题的方法,但这里有另一种可能更好的方法。它创建一个名为slide 的单独tk.Frame 来保存当前幻灯片的内容。这样做可以相对容易地迭代(仅)它的内容——它包含的“子”小部件——并销毁它们。

请注意如何将属性添加到名为num 的幻灯片框架中,以指示上次显示的幻灯片。这样做的好处是现在您不必在 slide_changer() 函数中声明它 global 来访问和更改其值,因为现在它是 slide 框架本身的一部分。

还要注意在为描述创建tk.Text 小部件时使用wrap=tk.WORD 关键字参数,该小部件应强制插入其中的文本在所有设备上正确格式化。这个小部件选项在一些 Tkinter documentation 中描述了关于 Text 小部件及其支持的各种选项。

import tkinter as tk
import time
import random

NUM_SLIDES = 2

root = tk.Tk()
root.title('Diffusion')

slide = tk.Frame(root, width=400, height=400)
slide.num = 0  # Add attribute to track slide being displayed.
slide.pack()

next_btn = tk.Button(root, text='NEXT')
next_btn.place(bordermode='inside', x=350, y=375)

def slide_changer():
    # Clear slide frame.
    for child in slide.winfo_children():
        child.destroy()

    slide.num += 1
    if slide.num > NUM_SLIDES:
        slide.num = 1  # Repeat starting at beginning.

    if slide.num == 1:  # Write text.
        description = tk.Text(slide, bd=0, height=5, width=50, wrap=tk.WORD)
        description.insert(tk.INSERT,
            'Diffusion is the net movement of particles from an area of higher '
            'concentration to an area of lower concentration, which results in '
            'the concentration being even. Here is an example, with the '
            'particles represented by orange dots:')
        description.place(x=0, y=190)

    elif slide.num == 2:  # Draw particles.
        canvas = tk.Canvas(slide, width=400, height=400)
        canvas.pack()
        particle = canvas.create_oval(10, 10, 20, 20, fill='orange')

next_btn.config(command=slide_changer)

root.mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多