1、Text的基本属性

#-*- encoding=utf-8 -*-

importtkinterfrom tkinter import *

if __name__ == '__main__':

win= tkinter.Tk() #窗口

win.title('南风丶轻语') #标题

screenwidth = win.winfo_screenwidth() #屏幕宽度

screenheight = win.winfo_screenheight() #屏幕高度

width = 500height= 300x= int((screenwidth - width) / 2)

y= int((screenheight - height) / 2)

win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) #大小以及位置

text=Text(

master=win, #父容器

bg='pink', #背景颜色

fg='red', #文本颜色

relief='sunken', #边框的3D样式 flat、sunken、raised、groove、ridge、solid。

bd=3, #边框的大小

height=3, #高度

width=20, #宽度

padx=1, #内间距,字体与边框的X距离

pady=1, #内间距,字体与边框的Y距离

state='normal', #设置状态 normal、active、 disabled

cursor='arrow', #鼠标移动时样式 arrow, circle, cross, plus...

font=('黑体', 20), #字体

wrap='char', #字数够width后是否换行 char, none, word

)

text.pack()

win.mainloop()

备注:

①支持的字体(通过tkinter.font.families获取)https://www.cnblogs.com/rainbow-tan/p/14043822.html/

②鼠标样式选项

"arrow", "circle", "clock", "cross", "dotbox", "exchange", "fleur", "heart", "man", "mouse", "pirate", "plus","shuttle", "sizing", "spider", "spraycan", "star","target", "tcross", "trek", "watch"

③边框样式,组件状态阅览

2、插入字符串和获取信息

# -*- encoding=utf-8 -*-

import tkinter

from tkinter import *

def end_insert():

text.insert('end', 'A') # 插入到末尾

def point_insert():

text.insert('insert', 'B') # 插入到光标处

def insert_x_y():

# 插入指定位置(x.y),1.0表示1行1列,1.2表示1行3列,行x从1开始,列y从0开始

# 如果x.y之前没内容,则添加到前面

text.insert(1.2, 'C')

def get():

# 获取输入的信息(x.y),1.0表示1行1列,1.2表示1行3列,行x从1开始,列y从0开始

msg = text.get(1.0, 1.6) # 获取1行1列至1行7列

print('1行1列至1行7列:{}'.format(msg))

msg = text.get(1.0, '1.end') # 获取1行1列至1行末尾

print('1行1列至1行末尾:{}'.format(msg))

msg = text.get(1.4, '2.end') # 获取1行5列至2行末尾

print('获取1行5列至2行末尾:{}'.format(msg))

msg = text.get(1.2, 'end') # 获取1行3列至内容结尾

print('获取1行3列至内容结尾:{}'.format(msg))

def delete():

text.delete(1.0, 1.6) # 删除1行1列至1行7列

text.delete(1.0, '1.end') # 删除1行1列至1行末尾

text.delete(1.4, '2.end') # 删除1行5列至2行末尾

text.delete(1.2, 'end') # 删除1行3列至内容结尾

if __name__ == '__main__':

win = tkinter.Tk() # 窗口

win.title('南风丶轻语') # 标题

screenwidth = win.winfo_screenwidth() # 屏幕宽度

screenheight = win.winfo_screenheight() # 屏幕高度

width = 500

height = 300

x = int((screenwidth - width) / 2)

y = int((screenheight - height) / 2)

win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置

text = Text(

master=win, # 父容器

bg='pink', # 背景颜色

fg='red', # 文本颜色

relief='sunken', # 边框的3D样式 flat、sunken、raised、groove、ridge、solid。

bd=3, # 边框的大小

width=5, # 宽度

height=5, # 高度

state='normal', # 设置状态 normal、readonly、 disabled

cursor='arrow', # 鼠标移动时样式 arrow, circle, cross, plus...

font=('黑体', 20), # 字体

wrap='char', # 字数够width后是否换行 char, none, word

)

text.pack()

Button(text='插入到末尾', command=end_insert).pack()

Button(text='插入到鼠标位置', command=point_insert).pack()

Button(text='插入到几行几列', command=insert_x_y).pack()

Button(text='获取输入的信息', command=get).pack()

Button(text='删除', command=delete).pack()

win.mainloop()

3、添加滚动条

#-*- encoding=utf-8 -*-

importtkinterfrom tkinter import *

if __name__ == '__main__':

win= tkinter.Tk() #窗口

win.title('南风丶轻语') #标题

screenwidth = win.winfo_screenwidth() #屏幕宽度

screenheight = win.winfo_screenheight() #屏幕高度

width = 800height= 600x= int((screenwidth - width) / 2)

y= int((screenheight - height) / 2)

win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) #大小以及位置

frame=Frame(win)

frame.pack()

s_y=Scrollbar(frame, )

s_y.pack(side=RIGHT, fill=Y)

s_x= Scrollbar(frame, orient=HORIZONTAL)

s_x.pack(side=BOTTOM, fill=X)

text=Text(

master=frame, #父容器

bg='pink', #背景颜色

fg='red', #文本颜色

relief='sunken', #边框的3D样式 flat、sunken、raised、groove、ridge、solid。

bd=3, #边框的大小

height=3, #高度

width=10, #宽度

padx=1, #内间距,字体与边框的X距离

pady=1, #内间距,字体与边框的Y距离

state='normal', #设置状态 normal、active、 disabled

cursor='arrow', #鼠标移动时样式 arrow, circle, cross, plus...

font=('黑体', 20), #字体

wrap='none', #字数够width后是否换行 char, none, word

yscrollcommand=s_y.set, #滚动条

xscrollcommand=s_x.set, #滚动条

)

s_y.config(command=text.yview)

s_x.config(command=text.xview)

text.pack()

win.mainloop()

备注:

①添加水平滚动条时需要保证wrap='none', 让字体不自动换行

4、插入组件和图片

#-*- encoding=utf-8 -*-

importthreadingimporttimeimporttkinterfrom tkinter import *

from PIL importImagefrom PIL importImageTkdefadd_btn():

btn= Button(text='按钮', relief='g', width=20)

text.window_create(INSERT, window=btn) #鼠标处插入

text.update() #更新

#text.window_create(1.2,window=btn)#1行3列插入

#text.update()

defdelete():

time.sleep(5)

btn.destroy()#销毁

thread= threading.Thread(target=delete)

thread.start()defadd_image():#图片需要为全局变量

text.image_create(INSERT, image=img)

text.update()if __name__ == '__main__':

win= tkinter.Tk() #窗口

win.title('南风丶轻语') #标题

screenwidth = win.winfo_screenwidth() #屏幕宽度

screenheight = win.winfo_screenheight() #屏幕高度

width = 500height= 300x= int((screenwidth - width) / 2)

y= int((screenheight - height) / 2)

win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) #大小以及位置

img= ImageTk.PhotoImage(Image.open('19.png'))

text=Text(

master=win, #父容器

bg='pink', #背景颜色

fg='red', #文本颜色

relief='groove', #边框的3D样式 flat、sunken、raised、groove、ridge、solid。

bd=3, #边框的大小

height=3, #高度

width=20, #宽度

padx=1, #内间距,字体与边框的X距离

pady=1, #内间距,字体与边框的Y距离

state='normal', #设置状态 normal、active、 disabled

cursor='arrow', #鼠标移动时样式 arrow, circle, cross, plus...

font=('黑体', 20), #字体

wrap='char', #字数够width后是否换行 char, none, word

)

text.pack()

Button(text='添加按钮', command=add_btn).pack()

Button(text='添加图片', command=add_image).pack()

win.mainloop()

备注:

①删除组件用组件.destroy()即可

python tk text 自动换行_Python tkinter之Text相关推荐

  1. tkinter中text属性_python tkinter基本属性详解

    1.外形尺寸 尺寸单位:只用默认的像素或者其他字符类的值!,不要用英寸毫米之类的内容. btn = tkinter.Button(root,text = "按钮")# 设置按钮尺寸 ...

  2. 用python设计学生管理系统_python+tkinter实现学生管理系统

    本文实例为大家分享了python+tkinter实现学生管理系统的具体代码,供大家参考,具体内容如下 from tkinter import * from tkinter.messagebox imp ...

  3. python调用计算器卡死_Python+tkinter使用40行代码实现计算器功能

    本文实例为大家分享了40行Python代码实现计算器功能,供大家参考,具体内容如下 偶尔用脚本写点东西也是不错的. 效果图 代码 from tkinter import * reset=True de ...

  4. python tk文本框_python图形界面tk 1.5 文本框(Text box | Entry)

    在tkinter中,文本框被称为Entry #!/usr/bin/env python # -*- coding: utf-8 -*- import tkinter as tk from tkinte ...

  5. python label怎么用_python tkinter label标签怎么使用?

    终于有机会给大家介绍了label标签内容,想必很多小伙伴已经迫不及待听小编说这个最常见的标签函数了吧,大家之所以喜欢,主要还是依赖于这个标签是我们每一次的编程必备,看着大家如此喜欢这个函数,一进入控件 ...

  6. python无师自通配套资源_Python Tkinter Pack布局管理器(超级详细,看了无师自通)...

    GUI 编程就相当于小孩子搭积木,每个积木块应该放在哪里,每个积木块显示为多大,也就是对大小和位置都需要进行管理,而布局管理器正是负责管理各组件的大小和位置的.此外,当用户调整了窗口的大小之后,布局管 ...

  7. python输入一个字母_python – Tkinter输入的第一个字母

    我的程序应该检查输入字的前三个字母是否与预定义的字相似. 我用Tkinter制作了一个GUI,想要得到输入字段的字母. 不知怎的,我不能像没有Tkinter那样实现它. 这就是我为shell做的方式: ...

  8. python提示对话框自动关闭_Python - tkinter:打开和关闭对话框窗口

    我是Python新手,必须编写一个简单的GUI程序,为了简单起见,我选择在tkinter中这样做. 我想要的GUI应该非常类似于在Windows上安装程序时经常遇到的对话框(您想要安装的位置,您想要的 ...

  9. python中grid函数_python tkinter中的grid布局是什么?

    之前跟大家讲过登录界面是怎么设置的,但是被大家吐槽了一番,原因是因为设置的窗口状态并不好看,大家拿来了公认为比较好看的登录界面,希望可以设置出一样的效果,在python里没有什么是不可能实现的,因此, ...

最新文章

  1. 初看Windows Media Center
  2. Oracle 一些常用的数据字典
  3. 2阶节IIR算法C语言源码
  4. 树莓派4上跑 .NET Core 3.0,这次,真·64位!
  5. MATLAB使用教程(4)——悄悄滴上手项目
  6. CentOS 7.2安装zabbix 3.0 LTS
  7. $$str php,php中 $$str 中 "$$" 的详解,php中str详解_PHP教程
  8. 初级第二课——统计总分
  9. 周题:UVa10736题。Foreign Exchange
  10. php 7中文手册pdf版,手册的格式 - PHP 7 中文文档
  11. HTTP 和 SOCKS 代理有什么区别,指纹浏览器搭配哪种代理合适
  12. dog log 算子_log算子和dog算子
  13. 前端如何学习,学习以后干什么?
  14. 2个月,3000节音频,利润30万+这就是赚钱的秘密
  15. 5G还没用上,4G却越来越慢了?
  16. PS(简单操作) 单张图片制作九宫格/证件照排版
  17. 投机之王杰西·利弗莫尔的经典炒股语录
  18. Linux环境下Nginx不支持中文文件名解决办法
  19. UVA 11426 GCD - Extreme (II) (欧拉函数)
  20. while及do while语句

热门文章

  1. 用PHP控制您的浏览器cache
  2. “忘恩负义”的浪胃仙,是个真狠人!
  3. 有赞再推视频号流量扶持政策 单商家单月最高可获5万流量奖励
  4. iFixit:手机屏幕底部安装小芯片致第三方维修iPhone 13屏幕更难
  5. 苹果可折叠iPhone有望在2023年推出 支持手写笔
  6. 研究称:苹果开始感受到全球芯片短缺影响,但三星等受影响更大
  7. 终于圆了天文梦!马化腾称腾讯将发布探星计划 还提到了《王者荣耀》
  8. 钟薛高雪糕最贵一支66元 创始人:成本40 你爱要不要
  9. 苹果正为iPhone 12开发磁性电池组 可为手机无线充电
  10. 苹果新隐私政策在iOS14.4测试版上线:能允许或拒绝“跟踪”