Py学习  »  Python

python学习:tkinter接口3-窗口控件

浮云的痛 • 3 年前 • 237 次点击  

【Checkbutton 多选】

import tkinter as tk

from tkmacosx import Button

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

l = tk.Label(window, bg='green', 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()

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()

【Scale 滑动条】

import tkinter as tk

from tkmacosx import Button

window = tk.Tk()

window.title('My window')

window.geometry('500x300')

l = tk.Label(window, bg='green', fg='white', 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_=0, to=10, orient=tk.HORIZONTAL, length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection)

s.pack()

# 从0开始,以2为刻度,精度为0.01

window.mainloop()

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/106376
 
237 次点击