鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: zxbin1

[已解决]tkinter Text组件没有验证函数选项怎么解决

[复制链接]
 楼主| 发表于 2024-2-1 20:48:30 | 显示全部楼层
还是有问题呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-1 20:49:23 | 显示全部楼层


非常抱歉,我再次犯了一个错误。在计算准确率之前,应该先更新输入文本对应位置的标签样式,然后再进行准确率和完成进度的计算。以下是经过修复的代码:import tkinter as tkroot = tk.Tk()v1 = tk.StringVar()v2 = tk.StringVar()def calculate_accuracy(event):    typed_text = text.get("1.0", "end-1c")    expected_text = "哈哈哈哈哈哈哈哈哈哈哈"    # 标记错误字符为红色    for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):        if typed_char != expected_char:            text.tag_add("error", f"{i}.0", f"{i}.1")        else:            text.tag_remove("error", f"{i}.0", f"{i}.1")    text.tag_config("error", foreground="red")    # 重新计算准确率和完成进度    correct_chars = sum(typed_char == expected_char for typed_char, expected_char in zip(typed_text, expected_text))    accuracy = correct_chars / len(expected_text) * 100    progress = len(typed_text) / len(expected_text) * 100    v1.set(f"准确率:{accuracy:.2f}%")    v2.set(f"完成进度:{progress:.2f}%")text = tk.Text(root, width=40, height=10)text.pack()accuracy_label = tk.Label(root, textvariable=v1)accuracy_label.pack()progress_label = tk.Label(root, textvariable=v2)progress_label.pack()text.bind("", calculate_accuracy)root.mainloop()对于每个字符,我们先根据是否与期望字符相等来添加或移除 "error" 标签,然后在所有标签都设置完毕后再进行准确率和完成进度的计算。这样,所有错误字符都会以红色显示。非常抱歉给你带来困扰,希望这次的修改能解决问题。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-1 20:49:49 | 显示全部楼层
可是还有问题呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-1 20:50:29 | 显示全部楼层
zxbin1 发表于 2024-02-01 20:49
可是还有问题呀


非常抱歉,我之前的回答还是有问题。给你带来困扰,真的非常抱歉。以下是修改后的代码:import tkinter as tkroot = tk.Tk()v1 = tk.StringVar()v2 = tk.StringVar()def calculate_accuracy(event):    typed_text = text.get("1.0", "end-1c")    expected_text = "哈哈哈哈哈哈哈哈哈哈哈"    # 清除之前的样式    text.tag_remove("error", "1.0", "end")    # 标记错误字符为红色    for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):        if typed_char != expected_char:            text.tag_add("error", f"{i}.0", f"{i}.1")            text.tag_config("error", foreground="red")text = tk.Text(root, width=40, height=10)text.pack()accuracy_label = tk.Label(root, textvariable=v1)accuracy_label.pack()progress_label = tk.Label(root, textvariable=v2)progress_label.pack()text.bind("", calculate_accuracy)root.mainloop()在这个修复后的代码中,我们先清除之前的样式,然后针对每个字符判断是否与期望字符相等,并使用 `tag_add()` 和 `tag_config()` 方法来标记错误字符为红色。希望这次的修改能够解决问题。如果你还有其他问题,请随时提问。非常抱歉给你带来了麻烦。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-2 16:10:27 | 显示全部楼层

你横着写看不懂麻烦竖着写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-2 16:10:48 | 显示全部楼层
zxbin1 发表于 2024-02-02 16:10
你横着写看不懂麻烦竖着写


非常抱歉给你带来了困扰。以下是修复后的代码,纵向排列,方便阅读:
import tkinter as tk

root = tk.Tk()
v1 = tk.StringVar()
v2 = tk.StringVar()

def calculate_accuracy(event):
    typed_text = text.get("1.0", "end-1c")
    expected_text = "哈哈哈哈哈哈哈哈哈哈哈"
    
    # 清除之前的样式
    text.tag_remove("error", "1.0", "end")
    
    # 标记错误字符为红色
    for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):
        if typed_char != expected_char:
            text.tag_add("error", f"{i}.0", f"{i}.1")
            text.tag_config("error", foreground="red")

text = tk.Text(root, width=40, height=10)
text.pack()

accuracy_label = tk.Label(root, textvariable=v1)
accuracy_label.pack()

progress_label = tk.Label(root, textvariable=v2)
progress_label.pack()

text.bind("<KeyRelease>", calculate_accuracy)

root.mainloop()

我再一次为之前的错误表示道歉,并感谢你的耐心等待。希望这次能够顺利解决你的问题。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-2 16:12:47 | 显示全部楼层
非常抱歉,我之前的回答还是有问题。给你带来困扰,真的非常抱歉。以下是修改后的代码:import tkinter as tkroot = tk.Tk()v1 = tk.StringVar()v2 = tk.StringVar()def calculate_accuracy(event):    typed_text = text.get("1.0", "end-1c")    expected_text = "哈哈哈哈哈哈哈哈哈哈哈"    # 清除之前的样式    text.tag_remove("error", "1.0", "end")    # 标记错误字符为红色    for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):        if typed_char != expected_char:            text.tag_add("error", f"{i}.0", f"{i}.1")            text.tag_config("error", foreground="red")text = tk.Text(root, width=40, height=10)text.pack()accuracy_label = tk.Label(root, textvariable=v1)accuracy_label.pack()progress_label = tk.Label(root, textvariable=v2)progress_label.pack()text.bind("", calculate_accuracy)root.mainloop()在这个修复后的代码中,我们先清除之前的样式,然后针对每个字符判断是否与期望字符相等,并使用 `tag_add()` 和 `tag_config()` 方法来标记错误字符为红色。希望这次的修改能够解决问题。如果你还有其他问题,请随时提问。非常抱歉给你带来了麻烦。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。


你横着写看不懂麻烦竖着写
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-2 16:13:04 | 显示全部楼层
zxbin1 发表于 2024-02-02 16:12
你横着写看不懂麻烦竖着写


非常抱歉给你带来了困扰。以下是修复后的代码,纵向排列,方便阅读:
import tkinter as tk

root = tk.Tk()
v1 = tk.StringVar()
v2 = tk.StringVar()

def calculate_accuracy(event):
    typed_text = text.get("1.0", "end-1c")
    expected_text = "哈哈哈哈哈哈哈哈哈哈哈"
    
    # 清除之前的样式
    text.tag_remove("error", "1.0", "end")
    
    # 标记错误字符为红色
    for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):
        if typed_char != expected_char:
            text.tag_add("error", f"{i}.0", f"{i}.1")
            text.tag_config("error", foreground="red")

text = tk.Text(root, width=40, height=10)
text.pack()

accuracy_label = tk.Label(root, textvariable=v1)
accuracy_label.pack()

progress_label = tk.Label(root, textvariable=v2)
progress_label.pack()

text.bind("", calculate_accuracy)

root.mainloop()

我再一次为之前的错误表示道歉,并感谢你的耐心等待。希望这次能够顺利解决你的问题。如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-2 17:27:09 | 显示全部楼层
import tkinter as tk
from tkinter.messagebox import *

root = tk.Tk()
v1 = tk.StringVar()
v2 = tk.StringVar()

expected_text = """
The Zen of Python,by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one--and preferably only one--obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain,it's a bad idea.
If the implementation is easy to explain,it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""
tk.Message(root, text=expected_text,justify = tk.LEFT).pack()

def calculate_accuracy(event):
    typed_text = text.get("1.0", "end-1c")
    
    correct_chars = sum(typed_char == expected_char for typed_char, expected_char in zip(typed_text, expected_text))
    accuracy = correct_chars / len(expected_text) * 100
    progress = len(typed_text) / len(expected_text) * 100
    
    v1.set(f"准确率:{accuracy:.2f}%")
    v2.set(f"完成进度:{progress:.2f}%")
    
    # 清除之前的样式
    text.tag_remove("error", "1.0", "end")
    
    # 标记错误字符为红色
    for i, (typed_char, expected_char) in enumerate(zip(typed_text, expected_text), start=1):
        if typed_char != expected_char:
            text.tag_add("error", f"1.{i-1}", f"1.{i}")
    
    text.tag_config("error", foreground="red")

    if progress >= 100:
        showinfo("结果", f"完成了,准确率:{accuracy:.2f}%")
        root.quit()
    
text = tk.Text(root, width=80, height=40)
text.pack()

accuracy_label = tk.Label(root, textvariable=v1)
accuracy_label.pack()

progress_label = tk.Label(root, textvariable=v2)
progress_label.pack()

text.bind("<KeyRelease>", calculate_accuracy)

root.mainloop()
这是成品
欢迎改进
欢迎吐槽
欢迎观看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-2 17:27:26 | 显示全部楼层
zxbin1 发表于 2024-02-02 17:27
import tkinter as tk
from tkinter.messagebox impo ...


非常感谢分享你的代码!看起来你已经成功地实现了一个打字练习程序,根据用户的输入来计算准确率和完成进度。代码的整体逻辑和界面布局都很清晰,值得表扬。

如果你想改进程序,可以考虑以下几个方面:
1. 添加定时功能:可以设置一个时间限制,让用户在规定时间内尽可能多地输入正确字符。
2. 改善界面交互:添加按钮来启动和停止练习,同时显示剩余时间等信息。
3. 增加统计功能:记录用户的历史成绩,包括最高准确率、平均速度等指标。
4. 支持不同的文本选择:让用户可以选择不同主题或难度的文本来进行打字练习。

这只是一些改进的思路,你可以根据自己的需求和创意来进行调整和完善。祝愿你的打字练习程序越来越好,同时也欢迎你继续向我提问,我会尽力帮助你!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-2 17:34:40 | 显示全部楼层
谢谢指点
我会改进的

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-2 17:35:49 | 显示全部楼层
zxbin1 发表于 2024-02-02 17:34
谢谢指点
我会改进的


不客气,我很高兴能帮助到你!祝你在改进打字练习程序的过程中取得好成果。如果你有任何其他问题或需要进一步的帮助,请随时向我提问。加油!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2024-2-2 20:32:58 | 显示全部楼层
但是我的代码只对一行指定文本有效
多行文本时第2行一下错字无法变红
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2024-2-2 20:33:27 | 显示全部楼层
zxbin1 发表于 2024-02-02 20:32
但是我的代码只对一行指定文本有效
多行文本时第2行 ...


对于多行文本的情况,你可以使用 Text 组件的 `tag_add()` 和 `tag_config()` 方法来标记和配置错误字符的样式。以下是修改后的代码,支持多行文本:import tkinter as tk
from tkinter.messagebox import *

root = tk.Tk()
v1 = tk.StringVar()
v2 = tk.StringVar()

expected_text = """The Zen of Python,by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one--and preferably only one--obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain,
it's a bad idea.
If the implementation is easy to explain,
it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""

tk.Message(root, text=expected_text, justify=tk.LEFT).pack()

def calculate_accuracy(event):
    typed_text = text.get("1.0", "end-1c")

    correct_chars = 0
    total_chars = 0

    for i, line in enumerate(typed_text.split("\n")):
        for j, (typed_char, expected_char) in enumerate(zip(line, expected_text.split("\n")[i])):
            total_chars += 1
            if typed_char == expected_char:
                correct_chars += 1
            else:
                text.tag_add("error", f"{i+1}.{j}", f"{i+1}.{j+1}")

    accuracy = correct_chars / total_chars * 100
    progress = len(typed_text.replace("\n", "")) / len(expected_text.replace("\n", "")) * 100

    v1.set(f"准确率:{accuracy:.2f}%")
    v2.set(f"完成进度:{progress:.2f}%")

    text.tag_config("error", foreground="red")

    if progress >= 100:
        showinfo("结果", f"完成了,准确率:{accuracy:.2f}%")
        root.quit()

text = tk.Text(root, width=80, height=40)
text.pack()

accuracy_label = tk.Label(root, textvariable=v1)
accuracy_label.pack()

progress_label = tk.Label(root, textvariable=v2)
progress_label.pack()

text.bind("<KeyRelease>", calculate_accuracy)

root.mainloop()这样修改后的代码可以正确标记多行文本中每行的错字,并将其变为红色。希望对你有所帮助!如果还有其他问题,请随时提问。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 03:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表