Tkinter 销毁 TopLevel window 并使用一个按钮在其位置创建一个新的
Tkinter destroy TopLevel window and create a new one in its place with one button
目前我正在尝试破坏顶层window(如果它在那里,如果不创建它)并产生一个取而代之的是新的。
目前,创建 window 的按钮会生成一个带有标签的小 window,仅显示基于 GUI 上用户输入的命令行。
然而,我尝试了谷歌搜索和大量不同的方法,但我无法让它破坏 window 并创建一个新的。
def view_command(): # Views Command
cmd_line_window = Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
#doing stuff here#
cmd_label = Label(cmd_line_window, text=example_cmd_output, foreground="white",
background="#434547")
cmd_label.config(font=("Helvetica", 16))
cmd_label.pack()
btn = Button(root, text="Click Me", command=view_command)
我已经尝试了一些 if
语句,一些 try
语句,但我似乎无法使其正常工作。我觉得有一种简单的方法可以在一个函数中执行此操作,但我似乎无法理解。
编辑: 目前,每次我单击按钮时,它都会更新我选择的所有变量并显示正确的内容,它只会不断产生更多 windows 更新后的标签。
Edit2: 我使用了评论中的建议和底部的答案来两全其美。这是我的代码,它打开 window 或更新标签,而不是根据 window 是否已经打开!
def view_command():
global cmd_label
example_cmd_output = acodec_stream_choices[acodec_stream.get()] \
+ encoder_dropdownmenu_choices[encoder.get()] + \
acodec_bitrate_choices[acodec_bitrate.get()] + \
acodec_channel_choices[acodec_channel.get()] + \
acodec_samplerate_choices[acodec_samplerate.get()] + \
acodec_gain_choices[acodec_gain.get()] + ac3_custom_cmd_input + ac3_title_input
try:
cmd_label.config(text=example_cmd_output)
except (AttributeError, NameError):
cmd_line_window = Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
cmd_label = Label(cmd_line_window, text=example_cmd_output, foreground="white", background="#434547")
cmd_label.config(font=("Helvetica", 16))
cmd_label.pack()
btn = Button(root, text="Click Me", command=view_command)
正如我在评论中提到的,您只需调用通用小部件 destroy()
方法即可删除任何可能已经存在的当前 Toplevel
。但是,由于您没有 post 您自己的代码试图这样做(但没有成功),这里有一个可运行的示例:
import tkinter as tk
def view_command(): # Views Command
global cmd_line_window
try:
cmd_line_window.destroy()
except (AttributeError, NameError):
pass
cmd_line_window = tk.Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
#doing stuff here#
cmd_label = tk.Label(cmd_line_window, text=example_cmd_output, foreground="white",
background="#434547")
cmd_label.config(font=("Helvetica", 16))
cmd_label.pack()
example_cmd_output = 'example cmd output'
root = tk.Tk()
btn = tk.Button(root, text="Click Me", command=view_command)
btn.pack()
root.mainloop()
更新
正如@acw1668 和我在其他评论中提到的,更好的方法是只创建 Toplevel
window 和 Label
一次 之后只需更新 Label
的文本(而不是每次单击 Button
时都销毁并重新创建所有内容。同样,try
/except
用来轻松实现这个:
import tkinter as tk
def view_command(): # Views Command
global cmd_line_window
global cmd_label
#doing stuff here#
example_cmd_output = 'example cmd output'
try:
cmd_label.config(text=example_cmd_output) # Update Label's text.
except (NameError, tk.TclError): # Label or Toplevel widget doesn't exist.
cmd_line_window = tk.Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
cmd_label = tk.Label(cmd_line_window, font=("Helvetica", 16),
foreground="white", background="#434547",
text=example_cmd_output)
cmd_label.pack()
root = tk.Tk()
btn = tk.Button(root, text="Click Me", command=view_command)
btn.pack()
root.mainloop()
目前我正在尝试破坏顶层window(如果它在那里,如果不创建它)并产生一个取而代之的是新的。
目前,创建 window 的按钮会生成一个带有标签的小 window,仅显示基于 GUI 上用户输入的命令行。
然而,我尝试了谷歌搜索和大量不同的方法,但我无法让它破坏 window 并创建一个新的。
def view_command(): # Views Command
cmd_line_window = Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
#doing stuff here#
cmd_label = Label(cmd_line_window, text=example_cmd_output, foreground="white",
background="#434547")
cmd_label.config(font=("Helvetica", 16))
cmd_label.pack()
btn = Button(root, text="Click Me", command=view_command)
我已经尝试了一些 if
语句,一些 try
语句,但我似乎无法使其正常工作。我觉得有一种简单的方法可以在一个函数中执行此操作,但我似乎无法理解。
编辑: 目前,每次我单击按钮时,它都会更新我选择的所有变量并显示正确的内容,它只会不断产生更多 windows 更新后的标签。
Edit2: 我使用了评论中的建议和底部的答案来两全其美。这是我的代码,它打开 window 或更新标签,而不是根据 window 是否已经打开!
def view_command():
global cmd_label
example_cmd_output = acodec_stream_choices[acodec_stream.get()] \
+ encoder_dropdownmenu_choices[encoder.get()] + \
acodec_bitrate_choices[acodec_bitrate.get()] + \
acodec_channel_choices[acodec_channel.get()] + \
acodec_samplerate_choices[acodec_samplerate.get()] + \
acodec_gain_choices[acodec_gain.get()] + ac3_custom_cmd_input + ac3_title_input
try:
cmd_label.config(text=example_cmd_output)
except (AttributeError, NameError):
cmd_line_window = Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
cmd_label = Label(cmd_line_window, text=example_cmd_output, foreground="white", background="#434547")
cmd_label.config(font=("Helvetica", 16))
cmd_label.pack()
btn = Button(root, text="Click Me", command=view_command)
正如我在评论中提到的,您只需调用通用小部件 destroy()
方法即可删除任何可能已经存在的当前 Toplevel
。但是,由于您没有 post 您自己的代码试图这样做(但没有成功),这里有一个可运行的示例:
import tkinter as tk
def view_command(): # Views Command
global cmd_line_window
try:
cmd_line_window.destroy()
except (AttributeError, NameError):
pass
cmd_line_window = tk.Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
#doing stuff here#
cmd_label = tk.Label(cmd_line_window, text=example_cmd_output, foreground="white",
background="#434547")
cmd_label.config(font=("Helvetica", 16))
cmd_label.pack()
example_cmd_output = 'example cmd output'
root = tk.Tk()
btn = tk.Button(root, text="Click Me", command=view_command)
btn.pack()
root.mainloop()
更新
正如@acw1668 和我在其他评论中提到的,更好的方法是只创建 Toplevel
window 和 Label
一次 之后只需更新 Label
的文本(而不是每次单击 Button
时都销毁并重新创建所有内容。同样,try
/except
用来轻松实现这个:
import tkinter as tk
def view_command(): # Views Command
global cmd_line_window
global cmd_label
#doing stuff here#
example_cmd_output = 'example cmd output'
try:
cmd_label.config(text=example_cmd_output) # Update Label's text.
except (NameError, tk.TclError): # Label or Toplevel widget doesn't exist.
cmd_line_window = tk.Toplevel()
cmd_line_window.title('Command Line')
cmd_line_window.configure(background="#434547")
cmd_label = tk.Label(cmd_line_window, font=("Helvetica", 16),
foreground="white", background="#434547",
text=example_cmd_output)
cmd_label.pack()
root = tk.Tk()
btn = tk.Button(root, text="Click Me", command=view_command)
btn.pack()
root.mainloop()