当前位置:   article > 正文

Tkinter极简实例(2020-08-12)——Entry篇_from tkinter import * root = tk() en = entry(root,

from tkinter import * root = tk() en = entry(root,font=('微软雅黑',20)) en.p

Tkinter Entry

Entry极简实例

from tkinter import *

root = Tk()

Entry(root, text='input your text here').pack()  # text毫无作用

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

Entry与变量

from tkinter import *

root = Tk()

e = StringVar()

entry = Entry(root, textvariable=e)
e.set('entry your text here')  # 设定初始文本
entry.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Entry设置只读

from tkinter import *

root = Tk()

e = StringVar()

entry = Entry(root, textvariable=e, state='readonly')
e.set('entry your text here')
entry.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

密码输入框

from tkinter import *

root = Tk()

Label(root, text='input your password below').pack()

e = StringVar()

entry = Entry(root, textvariable=e, show='*')
entry.pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

前景色/背景色/外观/宽度/高度/对齐方式/状态与Lable相同。

Tkinter CheckButton

多选按钮,可以表示两种状态:On或Off,可以设置回调函数,每当点击此按钮时回调函数被调用。

CheckButton极简例子

from tkinter import *

root = Tk()

Checkbutton(root, text='python').pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

CheckButton事件处理函数

from tkinter import *


def callback():
    print('checked this button')


root = Tk()

Checkbutton(root, text='python', command=callback).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

改变CheckButton的显示文本

from tkinter import *


def callback():
    v.set('change checkbutton')


root = Tk()

v = StringVar()
v.set('check button')
Checkbutton(root, text='python', command=callback, textvariable=v).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

将CheckButton的值与变量绑定

from tkinter import *


def callback():
    print(v.get())
    v.set(v.get())


root = Tk()

v = IntVar()
v.set('check button')
Checkbutton(root, text='python', command=callback, variable=v, textvariable=v).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

设置CheckButton的状态值

from tkinter import *


def callback():
    print(v.get())


root = Tk()

v = StringVar()
v.set('check button')
Checkbutton(root, text='python', command=callback, variable=v, onvalue='python', offvalue='tkinter').pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

Tkinter RadioButton

单选按钮

RadioButton极简例子

from tkinter import *

root = Tk()

Radiobutton(root, text='python').pack()
Radiobutton(root, text='tkinter').pack()
Radiobutton(root, text='widget').pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

为RadioButton指定组

from tkinter import *

root = Tk()

v = IntVar()
v.set(1)

for i in range(3):
    Radiobutton(root, variable=v, text='python', value=i).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

创建不同的组

from tkinter import *

root = Tk()

vLang = IntVar()
vOS = IntVar()
vLang.set(1)
vOS.set(2)
for v in [vLang,vOS]:
    for i in range(3):
        Radiobutton(root, variable=v, value=i, text='python' + str(i)).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

使用相同的value

from tkinter import *

root = Tk()

v = IntVar()
v.set(1)
for i in range(3):
    Radiobutton(root, variable=v, value=1, text='python' + str(i)).pack()
for i in range(3):
    Radiobutton(root, variable=v, value=i, text='python' + str(2 + i)).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

RadioButton绑定事件处理函数

from tkinter import *


def r1():
    print('call r1')


def r2():
    print('call r2' )


def r3():
    print('call r3')


def r4():
    print('call r4' )


i = 0

root = Tk()
v = IntVar()
v.set(0)
for r in [r1, r2, r3, r4]:
    Radiobutton(root, variable=v, text='radio button', value=i, command=r).pack()
    Radiobutton(root, variable=v, text='radio button', value=i, command=r).pack()

i += 1

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

RaidoButton外观效果

from tkinter import *

root = Tk()
v = IntVar()
v.set(1)
for i in range(3):
    Radiobutton(root, variable=v, indicatoron=0, text='python & tkinter', value=i).pack()

root.mainloop()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号