首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改字符文本小工具python tkinter的背景色

更改字符文本小工具python tkinter的背景色
EN

Stack Overflow用户
提问于 2018-04-26 22:34:36
回答 1查看 325关注 0票数 0
代码语言:javascript
复制
from tkinter import *
from tkinter import ttk
from tkinter import Tk

def Finddif():
    numb= 0
    for i, j in zip(t1.get('1.0', 'end-1c'), t2.get('1.0', 'end-1c')):
        if (i != j):
            numb = numb + 1
    lab2.config(text=numb)


root = Tk()
root.title("AJC")
root.resizable(width=FALSE, height=FALSE)
root.geometry("500x500+30+10")

lab2 = ttk.Label(root, text = "")
lab2.place(x = 100, y = 100, width = 180, height = 50)

t1 = Text(root)
scr1 = Scrollbar(root, command = t1.yview)
t1.config(yscrollcommand = scr1.set)
scr1.place(x = 585, y = 65, width = 50, height = 50)

t2 = Text(root)
scr2 = Scrollbar(root, command = t2.yview)
t2.config(yscrollcommand = scr2.set)
scr2.place(x = 585, y = 300, width = 50, height = 50)

t1.place(x = 20, y = 65, width = 100, height = 100)
t2.place(x = 20, y = 300, width = 100, height = 100)

b1 = ttk.Button(root, text = "Finddif", command = Finddif)
b1.place(x = 20, y = 30, width = 80, height = 25)

root.mainloop()

这是我的一个小程序。有2个文本和代码必须搜索差异和颜色的字符。标签在这种情况下不起作用,因为不知道哪里会是错误的字符。

EN

回答 1

Stack Overflow用户

发布于 2018-04-27 16:48:49

试着把你的努力分成更小的部分。解决了从GUI部分单独比较字符串的问题。

您的代码只计算差异的数量,但我认为您希望找出字符串中哪些字符相同,哪些不同:

代码语言:javascript
复制
string_1 = 'aaaaaaaa'
string_2 = 'aaXaaXXa'
result = ''

for index, char in enumerate(string_1):   # Loop through the string
    if char == string_2[index]: result = result + 'o'
    else: result = result + 'x'

print(result)     # prints: "ooxooxxo", where x means different.

注意:这个代码片段只适用于相同长度的字符串,但它的想法很重要。

在让它对不同大小的字符串正常工作后,您可以开始考虑在文本小部件中显示它们。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50045719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档