Python图形界面 Tkinter入门9

9.1鼠标按键


from tkinter import *

root= Tk()
root.title('主窗口')
root.geometry('400x400')


def clicker(event):                         #event ,传递按键
    myLabel = Label(root,text='已经点击'+ 'x:'+ str(event.x) +'\ty:'+str(event.y))
    myLabel.pack()


myButton = Button(root,text='点击')
#myButton.bind('<Button-3>',clicker)          #Button-3 鼠标右键
#myButton.bind('<Enter>',clicker)              #鼠标指针,碰到按钮就触发
myButton.bind('<Return>',clicker)              #鼠标指针,离开按钮就触发
myButton.pack()




root.mainloop()

9.2.1下拉框事件


from tkinter import *

root= Tk()
root.title('主窗口')
root.geometry('400x400')

def selected(event):
    if clicker.get() == '周三' :
        myLabel = Label(root, text='选择了周三').pack()
    else:
        myLabel = Label(root,text=clicker.get()).pack()


options = ['周一','周二','周三','周四','周五','周六','周日']


clicker = StringVar()
clicker.set(options[0])

drop = OptionMenu(root,clicker,*options,command=selected)
drop.pack(padx=10,pady=10)



#myButton = Button(root,text='从列表选择',command=selected).pack()




root.mainloop()

9.2.2.TTK下拉框事件


from tkinter import *
from tkinter import ttk

root= Tk()
root.title('主窗口')
root.geometry('400x400')

def selected(event):
    if drop.get() == '周三' :
        myLabel = Label(root, text='选择了周三').pack()
    else:
        myLabel = Label(root,text=drop.get()).pack()


options = ['周一','周二','周三','周四','周五','周六','周日']


drop = ttk.Combobox(root,values=options)
drop.current(0)
drop.bind('<<ComboboxSelected>>',selected)
drop.pack()





root.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值