当前位置:   article > 正文

Python tkinter(四) 文本框(Text)组件的属性说明及示例_python text

python text

Python tkinter 文本框组件用于tkinter GUI里添加文本、图片、按钮。

使用语法

widget = Text( master, parameter=value, ... )
  • master:文本框控件的父容器
  • parameter:文本框的参数
  • value:参数对应的值

各参数之间以逗号分隔。

参数说明:

height设置文本框的高度,高度值每加1则加一行
width设置文本框的宽度,宽度值每加1则加一个字节
insert文本框插入数据,可以指定插入数据的位置
delete删除文本框中的数据,可以通过数据位置,指定删除的数据
get获取文本框中的数据,可以通过数据位置,指定获取的数据
relief文本框样式,设置控件显示效果,可选的有:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。
bd设置文本框的边框大小,值越大边框越宽
bg设置文本框默认背景色
fg设置文本框默认前景色,即字体颜色
font文本字体,文字字号,文字字形。字形有overstrike/italic/bold/underline
state文本框状态选项,状态有DISABLED/NORMAL,DISABLED状态文本框无法输入,NORMAL状态可以正常输入
highlightcolor设置文本框点击后的边框颜色
highlightthickness设置文本框点击后的边框大小

说明:

文本框可以和按钮及图片结合,在文本框中插入按钮或图片。具体用法见代码示例:

代码示例:

  1. # -*- coding:utf-8 -*-
  2. from tkinter import *
  3. '''
  4. NW N NE
  5. W C E
  6. SW S SE
  7. '''
  8. class textbox:
  9. def __init__(self):
  10. self.root = Tk()
  11. self.root.title("文本框") # 设置窗口标题
  12. self.root.geometry("600x600") # 设置窗口大小 注意:是x 不是*
  13. '''文本框样式'''
  14. # 设置文本框高度为1,宽度为2,height=2表示一行的高度,width=2表示宽度为两个字节
  15. self.height_width_label = Label(self.root, text='文本框高度和宽度:')
  16. self.height_width_text = Text(self.root, height=2, width=10)
  17. # 文本框插入数据
  18. self.insert_label = Label(self.root, text='文本框插入字符:')
  19. self.insert_text = Text(self.root, height=1, width=10)
  20. self.insert_text.insert('0.0', '1234567890')
  21. # 文本框删除数据
  22. self.del_label = Label(self.root, text='文本框删除字符:')
  23. self.del_text = Text(self.root, height=1, width=10)
  24. self.del_text.insert('0.0', '123456789')
  25. self.button_del = Button(self.root, text='<==', command=self.delete_text)
  26. # 文本框获取数据
  27. self.get_label = Label(self.root, text='文本框获取字符')
  28. self.get_text = Text(self.root, height=1, width=10)
  29. self.get_text.insert('0.0', '1234567890')
  30. self.button_get = Button(self.root, text='获取字符', command=self.text_get)
  31. # 文本框样式
  32. self.relief_label = Label(self.root, text='文本框样式:')
  33. self.flat_text = Text(self.root, height=1, width=10, relief=FLAT)
  34. self.flat_text.insert('0.0', '边框平坦')
  35. self.sunken_text = Text(self.root, height=1, width=10, relief=SUNKEN)
  36. self.sunken_text.insert('0.0', '边框凹陷')
  37. self.taised_text = Text(self.root, height=1, width=10, relief=RAISED)
  38. self.taised_text.insert('0.0', '边框凸起')
  39. self.groove_text = Text(self.root, height=1, width=10, relief=GROOVE)
  40. self.groove_text.insert('0.0', '边框压线')
  41. self.tidge_text = Text(self.root, height=1, width=10, relief=RIDGE)
  42. self.tidge_text.insert('0.0', '边框脊线')
  43. # 文本框边框大小,bd='边框大小'
  44. self.db_label = Label(self.root, text='边框大小:')
  45. self.db_text = Text(self.root, height=1, width=10, bd=5)
  46. # 文本框后颜色,bg='背景色',fg='前景色'
  47. self.colour_label = Label(self.root, text='文本框颜色:')
  48. self.activebackground_text = Text(self.root, height=1, width=10, bg='blue')
  49. self.activeforeground_text = Text(self.root, height=1, width=10, fg='blue')
  50. # 文本框文字字体格式, font=('字体', 字号, 'bold/italic/underline/overstrike')
  51. self.font_Label = Label(self.root, text='显示边框样式:')
  52. self.font_text_1 = Text(self.root, height=1, width=20, font=('软体雅黑', 10, 'overstrike'))
  53. self.font_text_1.insert('0.0', '软体雅黑/10/重打印')
  54. self.font_text_2 = Text(self.root, height=1, width=20, font=('宋体', 10, 'italic'))
  55. self.font_text_2.insert('0.0', '宋体/10/斜体')
  56. self.font_text_3 = Text(self.root, height=1, width=20, font=('黑体', 10, 'bold'))
  57. self.font_text_3.insert('0.0', '黑体/10/加粗')
  58. self.font_text_4 = Text(self.root, height=1, width=20, font=('楷体', 10, 'underline'))
  59. self.font_text_4.insert('0.0', '楷体/10/下划线')
  60. # 文本框状态,禁用状态无法输入,正常状态可以输入
  61. self.state_Label = Label(self.root, text='文本框状态:')
  62. self.state_text_1 = Text(self.root, height=1, width=20)
  63. self.state_text_1.insert('0.0', '禁用状态')
  64. self.state_text_1.config(state=DISABLED)
  65. self.state_text_2 = Text(self.root, height=1, width=20)
  66. self.state_text_2.insert('0.0', '正常状态')
  67. self.state_text_2.config(state=NORMAL)
  68. # 文本框点击颜色,及颜色厚度。highlightcolor='颜色',highlightthickness=厚度
  69. self.label_colour = Label(self.root, text='点击颜色/厚度:')
  70. self.highlightcolor = Text(self.root, height=1, width=10, highlightcolor='red', highlightthickness=1)
  71. # 文本框插入按钮
  72. self.button_text_label = Label(self.root, text='文本框插入按钮:')
  73. self.button_text = Text(self.root, height=5, width=20)
  74. self.button_text.insert('0.0', '文本框插入按钮')
  75. self.text_button = Button(self.button_text, text='按钮', command=self.print_text)
  76. self.button_text.window_create(INSERT, window=self.text_button)
  77. # 文本框插入图片
  78. self.image_text_label = Label(self.root, text='文本框插入图片:')
  79. self.image_text = Text(self.root, height=12, width=30)
  80. self.image_text.insert('0.0', '\n文本框插入图片')
  81. photo = PhotoImage(file="1.gif")
  82. self.image_text.image_create('1.0', image=photo)
  83. '''grid布局'''
  84. self.height_width_label.grid(row=0, column=0, sticky=E)
  85. self.height_width_text.grid(row=0, column=1, sticky=W)
  86. self.insert_label.grid(row=1, column=0, sticky=E)
  87. self.insert_text.grid(row=1, column=1, sticky=W)
  88. self.del_label.grid(row=2, column=0, sticky=E)
  89. self.del_text.grid(row=2, column=1, sticky=W)
  90. self.button_del.grid(row=2, column=2, sticky=W)
  91. self.get_label.grid(row=3, column=0, sticky=E)
  92. self.get_text.grid(row=3, column=1, sticky=W)
  93. self.button_get.grid(row=3, column=2, sticky=W)
  94. self.relief_label.grid(row=4, column=0, sticky=E)
  95. self.flat_text.grid(row=4, column=1, sticky=W)
  96. self.sunken_text.grid(row=4, column=2, sticky=W)
  97. self.taised_text.grid(row=4, column=3, sticky=W)
  98. self.groove_text.grid(row=4, column=4, sticky=W)
  99. self.tidge_text.grid(row=4, column=5, sticky=W)
  100. self.db_label.grid(row=5, column=0, sticky=E)
  101. self.db_text.grid(row=5, column=1, sticky=W)
  102. self.colour_label.grid(row=6, column=0, sticky=E)
  103. self.activebackground_text.grid(row=6, column=1, sticky=W)
  104. self.activeforeground_text.grid(row=6, column=2, sticky=W)
  105. self.font_Label.grid(row=7, column=0, rowspan=2, sticky=E)
  106. self.font_text_1.grid(row=7, column=1, columnspan=2, sticky=W)
  107. self.font_text_2.grid(row=7, column=3, columnspan=2, sticky=W)
  108. self.font_text_3.grid(row=8, column=1, columnspan=2, sticky=W)
  109. self.font_text_4.grid(row=8, column=3, columnspan=2, sticky=W)
  110. self.state_Label.grid(row=9, column=0, sticky=E)
  111. self.state_text_1.grid(row=9, column=1, columnspan=2, sticky=W)
  112. self.state_text_2.grid(row=9, column=3, columnspan=2, sticky=W)
  113. self.label_colour.grid(row=10, column=0, sticky=E)
  114. self.highlightcolor.grid(row=10, column=1, sticky=W)
  115. self.button_text_label.grid(row=11, column=0, sticky=E)
  116. self.button_text.grid(row=11, column=1, columnspan=2, sticky=W)
  117. self.image_text_label.grid(row=12, column=0, sticky=E)
  118. self.image_text.grid(row=12, column=1, columnspan=3, sticky=W)
  119. self.root.mainloop()
  120. def print_text(self):
  121. print(self.button_text.get('0.0', 'end'))
  122. def delete_text(self):
  123. self.del_text.delete('0.0', END)
  124. def text_get(self):
  125. print(self.get_text.get('0.0', END))
  126. if __name__ == '__main__':
  127. textbox()

效果展示:

相关文档推荐:

Python tkinter(一) 按钮(Button)组件的属性说明及示例

python tkinter(二) 下拉框(combobox)组件的属性说明及示例

Python tkinter(三) 单选框(Radiobutton)组件的属性说明及示例

Python tkinter(五) 文本框(Entry)组件的属性说明及示例

Python tkinter(六) 标签(Label)组件的属性说明及示例

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/767609
推荐阅读
相关标签
  

闽ICP备14008679号