0
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()

Here is a little programm of mine. There are 2 texts and code have to search the difference and color the character. tag is not working in this situation because dont know where will be the wrong character.

  • 1
    Possible duplicate of [How to change the color of certain words in the tkinter text widget?](https://stackoverflow.com/questions/14786507/how-to-change-the-color-of-certain-words-in-the-tkinter-text-widget) – diegoiva Apr 26 '18 at 14:41
  • Can you please write for me. I saw that before but i couldn't write it for my code –  Apr 26 '18 at 14:43

1 Answers1

0

Try to divide your efforts into smaller bits. Solve the problem of comparing strings separately from the GUI part.

Your code only counts the number of differences but I think you want to find out which characters in the strings are the same and which are different:

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.

Note: this code snippet only works for strings of equal length, but it's the idea that's important.

After you have got this to function properly for strings of different sizes you can start to think about presenting them in a text widget.

figbeam
  • 7,001
  • 2
  • 12
  • 18