python带界面排序_排序的图形用户界面:如何在排序的每一步给特定的数字上色?...

请尝试使此代码适合您的应用程序。

它使用带有标记的Text小部件来生成彩色文本。因此,您应该用Text替换Label小部件。在from Tkinter import *

class Sorting(Frame):

def __init__(self):

Frame.__init__(self)

self.master.title("Sorting")

self.master.rowconfigure(5, weight=1)

self.master.columnconfigure(5, weight=1)

self.grid(sticky=W+E+N+S )

nums = [10, 20, 8, 5, 7] # example of entry

result = sorted(nums) # sorted result = [3 ,5 , 8, 10 ,20]

# the color list holds the items changing position when sortened

color = [ind for ind, (x, y) in enumerate(zip(nums, result)) if x != y]

entry_num = ''.join('%4i' % num for num in nums)

sort_nums = ''.join('%4i' % num for num in result)

l1 = Label(self, text="entry", width=25, height=1)

l1.grid(row=0, column=1, sticky=N)

t_entry = Text(self, width=25, height=2)

t_entry.grid(row=1, column=1, sticky=N)

t_entry.insert(END, entry_num)

l2 = Label(self, text='sorted', width=25, height=1)

l2.grid(row=2, column=1, sticky=N)

t_sorted = Text(self, width=25, height=2)

t_sorted.grid(row=3, column=1, sticky=N)

t_sorted.insert(END, sort_nums)

t_sorted.tag_config('red_tag', foreground='red')

for pos in color:

a = '1.%i' % (4 * pos)

b = '1.%i' % (4 * pos + 4)

t_sorted.tag_add('red_tag', a, b)

if __name__ == "__main__":

Sorting().mainloop()

nZas7.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值