我在 100 天代碼:Python 的第 89 天作業。我需要構建一個 Tkinter 應用程式來持續監控文本輸入,如果它檢測到 10 秒內沒有輸入任何內容,它將洗掉代碼。我嘗試使用以下代碼這樣做:
def get_wordcount(self):
start_num_char = len(self.entry_box.get(1.0, "end-1c"))
new_num_char = self.after(5000, self.get_wordcount)
if new_num_char <= start_num_char:
return True
else:
return False
我也試過:
def get_wordcount(self):
start_num_char = len(self.entry_box.get(1.0, "end-1c"))
new_num_char = self.after(5000, len(self.entry_box.get(1.0, "end-1c")))
if new_num_char <= start_num_char:
return True
else:
return False
問題是 new_num_char 等于“after#0”、“after#1”、“after#2”等,而不是等于新的字符數。如何每五秒獲取一次新字數?如果我做:
def get_wordcount(self):
start_num_char = len(self.entry_box.get(1.0, "end-1c"))
self.after(5000)
new_num_char = len(self.entry_box.get(1.0, "end-1c")
if new_num_char <= start_num_char:
return True
else:
return False
這只是凍結了整個視窗,直到五秒鐘后我才能在輸入框中輸入內容。我真的很感激這方面的一些幫助;幾天來,我一直在試圖弄清楚。完整代碼如下:
from tkinter import *
from tkinter.scrolledtext import ScrolledText
class App(Tk):
def __init__(self):
super().__init__()
self.title("Focus Writer")
self.background_color = "#EAF6F6"
self.config(padx=20, pady=20, bg=self.background_color)
self.font = "Arial"
self.start = False
self.time_left = 10
self.title_label = Label(text="Focus Writer",
bg=self.background_color,
font=(self.font, 26))
self.title_label.grid(row=0, column=0, columnspan=2)
self.explain = Label(text="Welcome to the Focus Writer app. You need to continuously input content in order to "
"keep the app from erasing your data. If the app detects that you haven't written "
"anything for more than 10 seconds, it will wipe everything, and you will need to "
"start over. \n\nPress the start button when you are ready to begin.\n",
bg=self.background_color,
font=(self.font, 18),
wraplength=850,
justify="left",)
self.explain.grid(row=1, column=0, columnspan=2)
self.start_img = PhotoImage(file="play-buttton.png")
self.start_button = Button(image=self.start_img,
height=50,
command=self.check_writing)
self.start_button.grid(row=2, column=0)
self.time_left_label = Label(text=self.time_left,
bg=self.background_color,
font=(self.font, 36))
self.time_left_label.grid(row=2, column=1)
self.entry_box = ScrolledText(width=80,
height=20,
wrap=WORD,
font=(self.font, 14))
self.entry_box.grid(row=3, column=0, columnspan=2)
def countdown(self):
if self.time_left > 0:
self.time_left -= 1
self.time_left_label.configure(text=self.time_left)
self.after(1000, self.countdown)
else:
if self.get_wordcount():
self.entry_box.delete(1.0, "end-1c")
else:
self.time_left = 0
return False
def get_wordcount(self):
start_num_char = len(self.entry_box.get(1.0, "end-1c"))
new_num_char = self.after(5000, len(self.entry_box.get(1.0, "end-1c")))
if new_num_char <= start_num_char:
return True
else:
return False
def check_writing(self):
self.start = True
self.start_button["state"] = DISABLED
self.entry_box.focus()
self.get_wordcount()
app = App()
app.mainloop()
uj5u.com熱心網友回復:
我會用另一種方式解決這個問題。創建函式會取消之前清除該函式的任何嘗試,然后安排在 10 秒內呼叫該函式。然后,創建一個在每次按鍵釋放時呼叫此按鈕的系結。整個機制只需要六行代碼。
這是一個例子:
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.after_id = None
text = tk.Text(self)
text.pack(fill="both", expand=True)
text.bind("<Any-KeyRelease>", self.schedule_clear_text)
def schedule_clear_text(self, event):
if self.after_id is not None:
self.after_cancel(self.after_id)
self.after_id = self.after(10000, event.widget.delete, "1.0", "end")
root = App()
root.mainloop()
uj5u.com熱心網友回復:
首先我會分成兩個功能
首先哪個設定self.start_num_char并self.get_wordcount()在一段時間后運行
第二個(self.get_wordcount) get new_num_char,比較self.start_num_char- 但它不使用return- 它直接重置計時器 -self.time_left = 10當條目中的文本更長(或更短)時。最后它保持并new_num_char在self.start_num_char短時間內再次運行
def start_wordcount(self):
self.start_num_char = len(self.entry_box.get(1.0, "end-1c"))
self.after(50, self.get_wordcount)
def get_wordcount(self):
new_num_char = len(self.entry_box.get(1.0, "end-1c"))
if new_num_char != self.start_num_char: # longer or shorter
self.time_left = 10
self.time_left_label.configure(text=self.time_left)
self.start_num_char = new_num_char
self.after(50, self.get_wordcount)
同時計數器顯示新時間和清潔entry時間self.time_left0
def countdown(self):
if self.time_left > 0:
self.time_left -= 1
self.time_left_label.configure(text=self.time_left)
self.after(1000, self.countdown)
else:
self.entry_box.delete(1.0, "end-1c")
self.start_num_char = 0
僅當您不鍵入時,此計數為 10 秒。
但它有一個小問題 - 因為counter和get_wordcount單獨運行所以有時會counter改變時間到9何時get_wordcount重置變數并且它可以顯示9但它不應該。也許它應該使用實時,它應該保持按下最后一個鍵的時間,并使用這個值來計算計數器。
from tkinter import *
from tkinter.scrolledtext import ScrolledText
class App(Tk):
def __init__(self):
super().__init__()
self.title("Focus Writer")
self.background_color = "#EAF6F6"
self.config(padx=20, pady=20, bg=self.background_color)
self.font = "Arial"
self.start = False
self.time_left = 10
self.title_label = Label(text="Focus Writer",
bg=self.background_color,
font=(self.font, 26))
self.title_label.grid(row=0, column=0, columnspan=2)
self.explain = Label(text="Welcome to the Focus Writer app. You need to continuously input content in order to "
"keep the app from erasing your data. If the app detects that you haven't written "
"anything for more than 10 seconds, it will wipe everything, and you will need to "
"start over. \n\nPress the start button when you are ready to begin.\n",
bg=self.background_color,
font=(self.font, 18),
wraplength=850,
justify="left",)
self.explain.grid(row=1, column=0, columnspan=2)
self.start_button = Button(text='Start',
command=self.check_writing)
self.start_button.grid(row=2, column=0)
self.time_left_label = Label(text=self.time_left,
bg=self.background_color,
font=(self.font, 36))
self.time_left_label.grid(row=2, column=1)
self.entry_box = ScrolledText(width=80,
height=20,
wrap=WORD,
font=(self.font, 14))
self.entry_box.grid(row=3, column=0, columnspan=2)
def countdown(self):
if self.time_left > 0:
self.time_left -= 1
self.time_left_label.configure(text=self.time_left)
self.after(1000, self.countdown)
else:
self.entry_box.delete(1.0, "end-1c")
self.start_num_char = 0
def start_wordcount(self):
self.start_num_char = len(self.entry_box.get(1.0, "end-1c"))
self.after(50, self.get_wordcount)
def get_wordcount(self):
new_num_char = len(self.entry_box.get(1.0, "end-1c"))
if new_num_char != self.start_num_char: # longer or shorter
self.time_left = 10
self.time_left_label.configure(text=self.time_left)
self.start_num_char = new_num_char
self.after(50, self.get_wordcount)
def check_writing(self):
self.start = True
self.start_button["state"] = DISABLED
self.entry_box.focus()
self.start_wordcount()
self.countdown()
app = App()
app.mainloop()
編輯:
您可以嘗試在文本更改時self.entry_box.bind('<<Modified>>', function)執行。function(event)但在我的 Linux 上,它只在第一次更改時運行。
編輯:
我添加time.time()了檢查時間之間的變化,現在它更好地顯示計數器。
def countdown(self):
if self.time_left > 0:
current_time = time.time()
if current_time >= self.change_time 1:
self.time_left -= 1
self.time_left_label.configure(text=self.time_left)
self.after(1000, self.countdown)
else:
self.entry_box.delete(1.0, "end-1c")
self.start_num_char = 0
def start_wordcount(self):
self.num_char = len(self.entry_box.get(1.0, "end-1c"))
self.change_time = time.time()
self.after(50, self.get_wordcount)
def get_wordcount(self):
new_num_char = len(self.entry_box.get(1.0, "end-1c"))
if new_num_char != self.num_char: # longer or shorter
self.time_left = 10
self.change_time = time.time()
self.time_left_label.configure(text=self.time_left)
self.num_char = new_num_char
self.after(50, self.get_wordcount)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/439070.html
