簡體   English   中英

更改角色文本小部件的背景顏色python tkinter

[英]Changing background color of character text widget python tkinter

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個文本和代碼必須搜索差異並為字符着色。 標簽在這種情況下不起作用,因為不知道錯誤的字符在哪里。

嘗試將您的努力分成較小的部分。 解決了將字符串與GUI部分分開進行比較的問題。

您的代碼僅計算差異的數量,但是我認為您想找出字符串中哪些字符相同,哪些不同:

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.

注意:此代碼段僅適用於長度相等的字符串,但是這一點很重要。

在使此功能對於不同大小的字符串正常運行之后,您可以開始考慮將其顯示在文本小部件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM