Python GUI编程:Tkinter模块之Entry控件

2022-08-0422:49:30编程语言入门到精通Comments807 views字数 3508阅读模式

  1. Entry控件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

Entry控件的作用是在键盘输入的文本信息,它的语法格式如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

1
my_entry = Entry(容器,可选项)

容器即内容放入的位置,可选项和上一节我们学习提到的Button中的可选项类似, 可选择的有:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

样式功能
bg和bd背景颜色和边框大小
cursor光标的形状
font文本字体
exportselection文本框内容是否复制功能
fg文字颜色
highlightcolor边框高亮的颜色
justify对齐方式
relief边框样式
selectbackground选择的文本背景颜色
selectborderwidth选择的文本背景边框宽度
selectforeground选择的文字颜色
state只读或可写控制
textvariable文本框的值
width文本框宽度
xscrollcommand水平方向滚动条

    2. Entry控件常用的方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

1) delete(first, last=None)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

删除first-last中的所有内容,如果使用delete(0,END)则删除输入框的所有内容文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

2)  get()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

获取输入框内的所有内容文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

3)  icursor(index)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

移动光标到index参数的位置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

4)  index(index)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

返回index参数对应的序号文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

5)  insert(index, text)文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

把text参数中的内容插入到索引为index的位置文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

6)  Select_clear()文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

清空文本框文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

7) xview ( index )文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

设置文本框链接的水平滚动条文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

3. Entry控件的使用文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

我们通过实例来使用一下Entry控件。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import tkinter
win = tkinter.Tk()
Frame_one = tkinter.Frame(win)#先创建一个容器放上面存放登录
Frame_one.pack(side = 'top')
Frame_two = tkinter.Frame(win)#再创建一个容器放中间存放密码
Frame_two.pack()
Frame_three = tkinter.Frame(win)#再创建一个容器在下面存放按钮
Frame_three.pack(side = 'bottom')
Label_one = tkinter.Label(Frame_one,text = '姓名:')
Lable_two = tkinter.Label(Frame_two,text = '密码:')
Entry_one = tkinter.Entry(Frame_one,bd = 5)
Entry_two = tkinter.Entry(Frame_two,bd = 5)
Button_one = tkinter.Button(Frame_three,text = '登录',activeforeground = 'red',activebackground = 'yellow',width = '7')
Button_one.pack(side = 'left')
Button_two = tkinter.Button(Frame_three,text = '注册',activeforeground = 'blue',activebackground = 'pink',width = '7')
Button_two.pack(side = 'right')
Label_one.pack(side = 'left')
Entry_one.pack(side = 'right')
Lable_two.pack(side = 'left')
Entry_two.pack(side = 'right')
win.mainloop()

输出结果为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

Python GUI编程:Tkinter模块之Entry控件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

我们首先在窗口中放置了三个容器,从上到下依次存放姓名、密码和按钮,然后再姓名后面放一个Entry控件,供我们输入姓名,在密码对应的后面放一个Enrty控件来输入密码,最后在下面放两个按钮分别提供登录和注册,最后我们把他们的位置放在左右一一对应。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

我们再通过函数的绑定来测试登录信息。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import tkinter as tk
import tkinter.messagebox
win = tk.Tk()
frame_name = tk.Frame(win)#创建容器来存放登陆的Label(文本框)与Entry(输入框)
frame_name.pack(side="top")#使该容器在页面的顶部
label_name = tk.Label(frame_name, text="Your Name : ")
label_name.pack(side="left")
entry_name = tk.Entry(frame_name, bd=5)
entry_name.pack(side="right")#在容器内创建Label与Entry,并使label在左,entry在右
# 下面的同理:
frame_password = tk.Frame(win)
frame_password.pack()
label_password = tk.Label(frame_password, text="Your Password : ")
label_password.pack(side="left")
entry_password = tk.Entry(frame_password, bd=5)
entry_password.pack(side="right")
def login():
    if entry_name.get() == "qy":
        if entry_password.get() == "dotcpp":
            print(tkinter.messagebox.showinfo("login""Success!"))
        else:
            print(tkinter.messagebox.showerror("login""Failed!"))
            entry_name.delete(0"end")
            entry_password.delete(0,"end")
    else:
        print(tkinter.messagebox.showerror("login""Failed!"))
        entry_name.delete(0"end")
        entry_password.delete(0,"end")
def signin():
    print(tkinter.messagebox.showerror("signin""Without Code!"))#必须先定义函数,否则点击按钮调用函数时会报函数不存在的错误
frame_button = tk.Frame(win)#创建容器以存放按钮
frame_button.pack(side="bottom")#使该容器位于页面最下方
button_login = tk.Button(
    frame_button,
    text="login",
    activeforeground="red",
    activebackground="yellow",
    width="7",
    command=login
# command的意思是执行已定义的函数,不可执行下文中出现的函数(未定义的函数)
)
button_login.pack(side="left")
# 下面的同理:
button_signin = tk.Button(
    frame_button,
    text="signin",
    activeforeground="blue",
    activebackground="pink",
    width="7",
    command=signin,
)
button_signin.pack(side="right")
win.mainloop()

输出的界面为:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

Python GUI编程:Tkinter模块之Entry控件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

当我们输入qy和dotcpp时如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

Python GUI编程:Tkinter模块之Entry控件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

如果输入其他内容显示如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

 文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

Python GUI编程:Tkinter模块之Entry控件文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/26544.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/26544.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定