python-tkinter学习
Posted Python_Heaven
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-tkinter学习相关的知识,希望对你有一定的参考价值。
5.radiobutton选择按钮
定义一个label作为提示框,
注意l.config()可以更新label中的text内容
tk.radiobutton()中传入的参数
import tkinter as tk
window = tk.Tk()
window.title("my window")
window.geometry('200x200')
var = tk.StringVar() # 定义一个变量
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
l.config(text='you have selected ' + var.get())
# 在window上显示为Option A,当选中时,将variable的var赋值为value:A
r1 = tk.Radiobutton(window, text='Option A', variable=var, value='A', command=print_selection)
r1.pack()
r2 = tk.Radiobutton(window, text='Option B', variable=var, value='B', command=print_selection)
r2.pack()
r3 = tk.Radiobutton(window, text='Option C', variable=var, value='C', command=print_selection)
r3.pack()
window.mainloop()
运行如图:
6.scale尺度-进度条
tk.Scale(),注意参数,描述,从5-11,from_下面有个杠,orient为方向,length为长度,展示值设置为不显示0表示false,单位长度tickinterval为3,保留小数位数resolution
注意:command有一个默认的传入值,为标度的值,v
import tkinter as tk
window = tk.Tk()
window.title("my window")
window.geometry('200x200')
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection(v):
l.config(text='you have selected ' + v)
s = tk.Scale(window, label='try me', from_=5, to=11, orient=tk.HORIZONTAL,
length=200, showvalue=0, tickinterval=3, resolution=0.01, command=print_selection)
s.pack()
window.mainloop()
运行如图:
7.checkbutton勾选项
tk.Checkbutton()表示勾选项,
注意传入参数:勾选上onvalue=1,未勾选offvalue=0,判断值进行函数label更新内容。
import tkinter as tk
window = tk.Tk()
window.title("my window")
window.geometry('200x200')
l = tk.Label(window, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
if (var1.get() == 1) and (var2.get() == 0):
l.config(text='I love only python.')
elif (var1.get() == 0) and (var2.get() == 1):
l.config(text='I love only C++.')
elif (var1.get() == 0) and (var2.get() == 0):
l.config(text='I do not love either.')
else:
l.config(text='I love both.')
var1 = tk.IntVar()
var2 = tk.IntVar()
c1 = tk.Checkbutton(window, text='Python', variable=var1, onvalue=1, offvalue=0, command=print_selection)
c2 = tk.Checkbutton(window, text='C++', variable=var2, onvalue=1, offvalue=0, command=print_selection)
c1.pack()
c2.pack()
window.mainloop()
运行如下:
8.Canvas画布
我们创建一个画布,在上面放图片,画线,画圆形,画方形,
并设置button,按一下,方形往下移动2个单位。
注意:放图片时设置位置,nw左上角为0,0。
import tkinter as tk
window = tk.Tk()
window.title("my window")
window.geometry("400x400")
canvas = tk.Canvas(window, bg='blue', height=200, width=400)
image_file = tk.PhotoImage(file='ins.gif')
image = canvas.create_image(0, 0, anchor='nw', image=image_file)
x0, y0, x1, y1 = 50, 50, 80, 80
line = canvas.create_line(x0, y0, x1, y1) # 画个线
oval = canvas.create_oval(x0, y0, x1, y1, fill='red') # 画个圆形
arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=0,extent=180) # 画个扇形,从0度打开到180度
rect = canvas.create_rectangle(100,30,120,50) # 正方形
canvas.pack()
def moveit():
# 把正方形往下移动
canvas.move(rect,0,2) # 移动rect,x不移动,y每次移动2个单位
b = tk.Button(window, text='move', command=moveit).pack()
window.mainloop()
运行如图:
以上是关于python-tkinter学习的主要内容,如果未能解决你的问题,请参考以下文章