美文网首页
python学习:tkinter接口3-窗口控件

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

作者: 0清婉0 | 来源:发表于2021-01-17 19:35 被阅读0次

【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学习:tkinter接口3-窗口控件

      本文链接:https://www.haomeiwen.com/subject/zokzaktx.html