Tkinter编程应知应会(7)-创建和使用标签控件

网友投稿 540 2022-09-06

Tkinter编程应知应会(7)-创建和使用标签控件

Tkinter编程应知应会(7)-创建和使用标签控件

还是首先看视频。

视频中一共构建了两个标签。第一个用于显示属性变更后的状态。

# create a label to change state.label = Label(root,text="ColorLabel", background="#ffffa0",foreground="#ff0000", activebackground="#a0ffa0",activeforeground="#0000ff", font=ftiTimes, height=1, width=20)label.grid(row=0, column=0, columnspan=4)

这个标签和前一篇文章中构建状态按钮的代码除了控件的类型由Button变为Label以外,其他完全一样。实际上这也意味着不同的控件之间有许多相同的属性,这会带来许多便利性。

接下来是前一篇文章中差不多一样的change_state函数和按钮。

# change state function.def change_state(): state = label.cget('state') if state==DISABLED: label.config(state=NORMAL) elif state==NORMAL: label.config(state=ACTIVE) else: label.config(state=DISABLED)# change state button.Button(root,text="State", command=change_state).grid(row=1, column=0, sticky=E+W)

接下来设计一个用来调整控件高度的按钮。它的处理是从1到4循环调整标签的高度。需要注意的是高度的数值是文字的行数。

# change label height function.def change_height(): current_height = label.cget('height') if current_height == 4: current_height = 1 else: current_height = current_height + 1 label.config(height=current_height)# change state button.Button(root,text="Height", command=change_height).grid(row=1, column=1, sticky=E+W)

调整宽度的处理和调整高度处理差不多。这里宽度的单位是当前字体一个字符的宽度。

# change width.def change_width(): width = label.cget('width') if width == 20: width = 1 else: width = width + 1 label.config(width=width)# change width button.Button(root,text="Width", command=change_width).grid(row=1, column=2, sticky=E+W)

切换文字列的处理。这里使用的是修改text属性的方式。

# change label_textdef change_text(): text = label.cget('text') if text=="ColorLabel": label.config(text='Demo') else: label.config(text='ColorLabel')# change text button.Button(root,text="Text", command=change_text).grid(row=1, column=3, sticky=E+W)

构建另一个多行标签:

# create fontftTimes = Font(family='Times', size=12, weight=BOLD)# create text variable.str_var = StringVar()str_var.set("You can limit the number of characters in each" "line by seting this option tothedesirednumber.")# create a label to change state.m_label = Label(root,font=ftTimes, height=3, wraplength=250, textvariable=str_var)m_label.grid(row=2, column=0, columnspan=4)

略有不同的是标签的字符串不是通过text属性指定,而是通过一个StringVar类型的变量指定的。这种方式的好处是一旦完成设定,将来所有修改变量的结果都会直接反映到标签控件上。在实际的开发中,也可以使用另外一个已经存在的变量。这应该是这种方式的本意。

wraplength属性用来指定多行文字列折返的宽度。文档中的说明的是字符单位,但从目前作者环境的执行结果来看好像是像素单位。

underline属性可以在标签文字列中选择一个字符添加下划线。下面是向左移动下划线的处理。当下划线移动到第0个字符的时再向左移动时,下划线会出现在最后一个字符的下面。

# change underline to previous char.def prev_underline(): text = m_label.cget('text') underline = m_label.cget('underline') if underline == -1: m_label.config(underline=len(text) - 1) else: underline = underline - 1 if underline == -1: m_label.config(underline=len(text) - 1) else: m_label.config(underline=underline)# change underline to previous char button.Button(root,text='PrevChar', command=prev_underline).grid(row=3, column=0, sticky=E+W)

向右移下划线的代码和向左移差不多:

# change underline to next char.def next_underline(): text = m_label.cget('text') underline = m_label.cget('underline') if underline == -1: m_label.config(underline=0) else: underline = underline + 1 if underline>=len(text): m_label.config(underline=0) else: m_label.config(underline=underline)# change underline to next char button.Button(root,text='NextChar', command=next_underline).grid(row=3, column=1, sticky=E+W)

左移和右移下划线的感觉和移动光标差不多,接下来可以使用下面的代码删除当前字符。

# delete char.def delete_char(): text = str_var.get() underline = m_label.cget('underline') if underline >=0 and underline < len(text): str_var.set(text[0 : underline] + text[underline + 1:])# change state button.Button(root,text='DeleteChar', command=delete_char).grid(row=3, column=2, sticky=E+W)

最后是调整文字对齐方式的代码,这个功能只在多行条件下可用。

# change state justify.def change_justify(): justify = m_label.cget('justify') if justify==LEFT: m_label.config(justify=CENTER) elif justify==CENTER: m_label.config(justify=RIGHT) else: m_label.config(justify=LEFT)# change state button.Button(root,text="Justify", command=change_justify).grid(row=3, column=3, sticky=E+W)

完整代码可以从以下链接-:

​​https://github.com/xueweiguo/TkinterPrimer/blob/master/Sample/7%20label.py​​

觉得本文有帮助?请分享给更多人。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:荣耀笔记本电脑MagicBook Pro 恢复出厂设置
下一篇:内存中OLTP常见的实施场景(OLTP场景)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~