python--tkinter03

1、scale 滚动条

import tkinter as tk
window = tk.Tk()
window.title("The window")
window.geometry("400x300")

l = tk.Label(window,bg='yellow',text='empty')
l.pack()

def print_selection(v):
    #这里的参数v 即滚动条定位的数据
    #config 修改text里面的内容
    l.config(text='you have selected ' + v)

#滑块 scale ,label scale部件的名称,from_ to 从1到20值范围滑动,
# orinent tk.HORIZONTAL 在这里就是设置滚动条的放心,为横向
#resolution 0.01 保留几位小数 保留2位
#showvalue  0 不显示数字 1显示数字,tickinterval 2 间隔为多少
s = tk.Scale(window,label='try me',from_=1,to=11,orient=tk.HORIZONTAL,
             length=400,showvalue=1,tickinterval=2,resolution=0.01,command=print_selection)
s.pack()
window.mainloop()
 
 
2、Checkbutton
#checkbutton 勾选项
import tkinter as tk
window = tk.Tk()
window.title("The window")
window.geometry("400x300")

l = tk.Label(window,bg='red',width=20,text='empty')
l.pack()

def print_selection():
    if(var1.get() == 1) & (var2.get() == 0):
        l.config(text="I love only python ")
    elif(var1.get() == 0) & (var2.get() == 1):
        l.config(text="I love only C++ ")
    elif(var1.get() == 0) & (var2.get() == 0):
        l.config(text = "I do not love either")
    else:
        l.config(text='I love both')
        
var1 = tk.IntVar()
var2 = tk.IntVar()
#创建checkbutton部件,参数onvalue和前面讲的部件radiobutton中的value相似, 
#当我们选中了这个checkbutton,onvalue的值1就会放入到var1中, 然后var1将其赋值给参数variable,
#offvalue用法相似,但是offvalue是在没有选中这个checkbutton时,offvalue的值1放入var1,
#然后赋值给参数variable 这是创建一个checkbutton部件,以此类推,可以创建多个checkbutton
c1 = tk.Checkbutton(window,text='Python',variable=var1,onvalue=1,offvalue=0,
                   command=print_selection)
c1.pack()
c2 = tk.Checkbutton(window,text='C++',variable=var2,onvalue=1,offvalue=0,
                   command=print_selection)
c2.pack()
window.mainloop()

猜你喜欢

转载自blog.csdn.net/qq_31307013/article/details/79833618
今日推荐