当前位置:   article > 正文

python tkinter 使用(八)_python tkinter.simpledialog.simpledialog

python tkinter.simpledialog.simpledialog

python tkinter 使用(八)

本文主要讲下tkinter库中的其他的一些模块,如simpleDialog,scrollerText等.

1: simpleDialog

simpledialog提供一些简单的对话框,用于获取用户输入.

  1. askinteger(title, prompt, **kw):
  2. askfloat(title, prompt, **kw):
  3. askstring(title, prompt, **kw):

title为对话框的标题,prompt为提示信息,**kw为可选参数,用于设置对话框的其他属性.

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
 @Author: zh
 @Time 2023/11/27 上午10:11  .
 @Email: 
 @Describe:
"""
import tkinter as tk
import tkinter.simpledialog as sd
# 创建窗口
root = tk.Tk()
root.title("simpleDialog")
root.geometry("500x500")

def askinteger(event):
    result = sd.askinteger("this is askinteger","content")
    print('simpleDialog',result)

def askfloat(event):
    result = sd.askfloat("this is askfloat","content")
    print('simpleDialog',result)

def askstring(event):
    result = sd.askstring("this is askstring","content")
    print('simpleDialog',result)

#内部会校验输入的内容是否合法
btn = tk.Button(text="askinteger")
btn.bind('<1>',askinteger)
btn.pack()

btn = tk.Button(text="askfloat")
btn.bind('<1>',askfloat)
btn.pack()

btn = tk.Button(text="askstring")
btn.bind('<1>',askstring)
btn.pack()


root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

2: colorchooser

tkinter模块内置的颜色选择器,可以让用户选择颜色.

def colorselect():
    color = tk.colorchooser.askcolor()
    print(color)

tk.Button(root, text="colorselect", command=colorselect).pack()
  • 1
  • 2
  • 3
  • 4
  • 5

3: scrolledText

tkinter.scrolledText 是一个带有滚动条的多行文本框。它可以用于显示和编辑大量文本.

import tkinter as tk
from tkinter.scrolledtext import ScrolledText

text =ScrolledText(root, width=50, height=10)
text.pack(side=tk.LEFT, expand=True)

def insertScrollText():
    result = sd.askstring("this is askstring", "请输入插入scrollerText的内容")
    text.insert(tk.END,result)

tk.Button(root, text="insert", command=insertScrollText).pack()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

ScrolledText小部件的insert()方法向其中添加文本. 具体调用如insertScrollText.

另外可以修改font 属性来设置scrolledText的文字大小,fg来设置文字颜色.

  1. .tag_add(tagname, start, end) :给指定范围内的文本添加标签,其中tagname为标签名,startend为文本范围的起始和结束位置。

  2. tag_remove(tagname, start, end):移除指定范围内的文本标签,其中 tagname为标签名,start和end为文本范围的起始和结束位置。

#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
 @Author: zh
 @Time 2023/11/27 上午11:40  .
 @Email: 
 @Describe:
"""
import tkinter as tk
from tkinter.scrolledtext import ScrolledText
# 创建窗口
root = tk.Tk()
root.title("ScrolledText")
root.geometry("500x500")

text2 = ScrolledText(root, width=30, height=10)
text2.grid(column=0, row=0)
text2.insert(tk.INSERT, "Hello, world!")
 # 给"Hello"添加标签
text2.tag_add("tag1",1.0,1.5)
# 设置标签样式
text2.tag_config("tag1",background="green")
# 移除标签
text2.tag_remove("tag1",1.3,1.5)
root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  1. tag_config: 配置指定标签的样式
text =ScrolledText(root, width=30, height=10,font=("Helvetica", 12), fg="blue")
text.pack(side=tk.LEFT, expand=True)
text.tag_config("font", font=("Helvetica", 20))
text.tag_config("color", foreground="red")

def insertScrollText():
    result = sd.askstring("this is askstring", "请输入插入scrollerText的内容")
    text.insert(tk.END, result)
    text.insert(tk.END,result,"font")
    text.insert(tk.END, result, "color")

tk.Button(root, text="insert", command=insertScrollText).pack()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/874768
推荐阅读
相关标签
  

闽ICP备14008679号