【问题标题】:How do I destroy a tkinter frame?如何销毁 tkinter 框架?
【发布时间】:2021-08-09 17:02:04
【问题描述】:

我正在尝试制作一个包含输入字段和提交按钮的 tkinter 框架。当按下提交按钮时,我想将输入字符串传递给程序并销毁框架。经过多次实验,我想出了这个脚本:

from tkinter import *
from tkinter import ttk
import time

root = Tk()
entryframe = ttk.Frame(root)
entryframe.pack()
par = StringVar('')
entrypar = ttk.Entry(entryframe, textvariable=par)
entrypar.pack()
submit = ttk.Button(entryframe, text='Submit', command=entryframe.quit)
submit.pack()
entryframe.mainloop()
entryframe.destroy()
parval = par.get()
print(parval)
time.sleep(3)
root.mainloop()

当“提交”按钮被按下时,参数值被正确地传递给脚本并被打印出来。但是,入口帧仅在 3 秒后被销毁(由 time.sleep 函数设置)。 我想立即销毁入口框架。 我有一个稍微不同的脚本版本,其中入口框架确实立即被破坏(尽管按钮本身没有被破坏),但 par 的值没有被打印出来:

from tkinter import *
from tkinter import ttk
import time

root = Tk()
entryframe = ttk.Frame(root)
entryframe.pack()
par = StringVar('')
entrypar = ttk.Entry(entryframe, textvariable=par)
entrypar.pack()
submit = ttk.Button(root, text='Submit', command=entryframe.destroy)
submit.pack()
entryframe.mainloop()
# entryframe.destroy()
parval = par.get()
print(parval)
time.sleep(3)
root.mainloop()

我怎样才能得到两个动作,即立即销毁入口框架并打印 par 的值?

【问题讨论】:

  • 在使用tkinter 时绝对不要使用time.sleep,除非你真的知道自己在做什么
  • 你应该只使用一个mainloop(),当你点击按钮时,你应该运行从Entry获取值的函数,显示它或将它放在某个全局变量而不是StringVar中,然后销毁窗户。 Button(command=my_function_to_get_data)

标签: python tkinter tkinter-entry


【解决方案1】:

请注意 100% 确定您要执行的操作,但请查看以下代码:

from tkinter import *
from tkinter import ttk

def print_results():
    global user_input # If you want to access the user's input from outside the function
    # Handle the user's input
    user_input = entrypar.get()
    print(user_input)
    # Destroy whatever you want here:
    entrypar.destroy()
    submit.destroy()
    # If you want you can also destroy the window: root.destroy()

    # I will create a new `Label` with the user's input:
    label = Label(root, text=user_input)
    label.pack()

# Create a tkitner window
root = Tk()

# Create the entry
entrypar = ttk.Entry(root)
entrypar.pack()

# Create the button and tell tkinter to call `print_results` whenever
# the button is pressed
submit = ttk.Button(root, text="Submit", command=print_results)
submit.pack()

# Run tkinter's main loop
# It will stop only when all tkinter windows are closed
root.mainloop()

# Because of the `global user_input` now we can use:
print("Again, user_input =", user_input)

我定义了一个会破坏入口和按钮的函数。它还会创建一个显示用户输入的新标签。

【讨论】:

    【解决方案2】:

    我能够使用 wait_window 方法完成我想要的。这是正确的脚本:

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    entryframe = ttk.Frame(root)
    entryframe.pack()
    entrypar = ttk.Entry(entryframe)
    entrypar.pack()
    submit = ttk.Button(entryframe, text='Submit', command=entryframe.destroy)
    submit.pack()
    entrypar.wait_window()
    parval = entrypar.get()
    print(parval)
    close_button = ttk.Button(root, text='Close', command=root.destroy)
    close_button.pack()
    root.mainloop()
    

    我的意图在我最初的问题中并未完全清楚,对此我深表歉意。无论如何,答案确实让我走上了正轨,我非常感谢。

    【讨论】:

      猜你喜欢
      • 2011-06-17
      • 1970-01-01
      • 2020-02-22
      • 2021-06-12
      • 2016-05-03
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多