在应用仍处于运行状态时更改 Tkinter 标签 运行
Changing Tkinter label while app is still running
在其他帖子中找不到我的问题的确切答案。我正在寻找的是更新标签的方法,而我的另一个功能是 运行ning。我尝试先更改标签,然后调用 my_function(),但标签仍然没有更新,但是 my_function() 是 运行ning 并在终端中打印结果。我是 Tkinter 的新手,据我所知,虽然我们没有点击 window.mainloop() 我的标签不会更新。有什么方法可以在其他功能 运行ning 时更新标签?
import os
import tkinter as tk
from tkinter import END, Label, Scrollbar, Text, filedialog
def my_function(directory: str) -> None:
for item in os.scandir(directory):
if item.is_file:
# print(f'File name: {item.name}')
text_area.insert(END, f'File name: {item.name}\n')
def select_folder() -> None:
'''
Tkinter function for button,
user can select folder.
'''
path = filedialog.askdirectory()
status.config(text='Status: Work in progress, please wait!')
text_area.insert(END, 'Visited folders:\n')
my_function(path)
status.config(text='Status: Done!')
# Start the app window
window = tk.Tk()
window.title('PDF maker')
window.geometry('400x400')
# Status Label
status = Label(window, text='Status: Select the folder')
status.pack()
# Button for selecting folder
button = tk.Button(window, text='Select Folder', command=select_folder)
button.pack(side='bottom', pady=30)
# Horizontal and Vertical Scrollbars
v_s = Scrollbar(window)
v_s.pack(side='right', fill='y')
h_s = Scrollbar(window, orient='horizontal')
h_s.pack(side='bottom', fill='x')
# Text area for result output
text_area = Text(
window,
wrap='none',
font=('Times New Roman', 13),
yscrollcommand=v_s.set,
xscrollcommand=h_s.set
)
text_area.pack(padx=10, pady=10, expand=True, fill='both')
# Adding scrollability to text
v_s.config(command=text_area.yview)
h_s.config(command=text_area.xview)
window.mainloop()
更新
还是应该为一个按钮创建两个功能?第一个函数将更改标签和文本,第二个函数将 运行 my_func().
一如既往,答案很简单。
试试这个:
#...
def select_folder() -> None:
"""
Tkinter function for button,
user can select folder.
"""
status.config(text="Status: Work in progress, please wait!") # switched lines here
path = filedialog.askdirectory()
text_area.insert(END, "Visited folders:\n")
my_function(path)
status.config(text="Status: Done!")
#...
以前它没有按预期工作,因为 filedialog.askdirectory()
阻止了 program-flow(类似于 input()
)。
在其他帖子中找不到我的问题的确切答案。我正在寻找的是更新标签的方法,而我的另一个功能是 运行ning。我尝试先更改标签,然后调用 my_function(),但标签仍然没有更新,但是 my_function() 是 运行ning 并在终端中打印结果。我是 Tkinter 的新手,据我所知,虽然我们没有点击 window.mainloop() 我的标签不会更新。有什么方法可以在其他功能 运行ning 时更新标签?
import os
import tkinter as tk
from tkinter import END, Label, Scrollbar, Text, filedialog
def my_function(directory: str) -> None:
for item in os.scandir(directory):
if item.is_file:
# print(f'File name: {item.name}')
text_area.insert(END, f'File name: {item.name}\n')
def select_folder() -> None:
'''
Tkinter function for button,
user can select folder.
'''
path = filedialog.askdirectory()
status.config(text='Status: Work in progress, please wait!')
text_area.insert(END, 'Visited folders:\n')
my_function(path)
status.config(text='Status: Done!')
# Start the app window
window = tk.Tk()
window.title('PDF maker')
window.geometry('400x400')
# Status Label
status = Label(window, text='Status: Select the folder')
status.pack()
# Button for selecting folder
button = tk.Button(window, text='Select Folder', command=select_folder)
button.pack(side='bottom', pady=30)
# Horizontal and Vertical Scrollbars
v_s = Scrollbar(window)
v_s.pack(side='right', fill='y')
h_s = Scrollbar(window, orient='horizontal')
h_s.pack(side='bottom', fill='x')
# Text area for result output
text_area = Text(
window,
wrap='none',
font=('Times New Roman', 13),
yscrollcommand=v_s.set,
xscrollcommand=h_s.set
)
text_area.pack(padx=10, pady=10, expand=True, fill='both')
# Adding scrollability to text
v_s.config(command=text_area.yview)
h_s.config(command=text_area.xview)
window.mainloop()
更新
还是应该为一个按钮创建两个功能?第一个函数将更改标签和文本,第二个函数将 运行 my_func().
一如既往,答案很简单。
试试这个:
#...
def select_folder() -> None:
"""
Tkinter function for button,
user can select folder.
"""
status.config(text="Status: Work in progress, please wait!") # switched lines here
path = filedialog.askdirectory()
text_area.insert(END, "Visited folders:\n")
my_function(path)
status.config(text="Status: Done!")
#...
以前它没有按预期工作,因为 filedialog.askdirectory()
阻止了 program-flow(类似于 input()
)。