会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133764个问题
Python 全系列/第二阶段:Python 深入与提高/文件处理 2012楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 2013楼

我电脑上运行视频上的代码时是单击鼠标中键才有反应的

# coding=utf-8
# 测试 多种事件绑定方式汇总
from tkinter import *
root = Tk();root.geometry("270x50")
def mouseTest1(event):
    print("bind()方式绑定,可以获取 event 对象")
    print(event.widget)
def mouseTest2(a,b):
    print("a={0},b={1}".format(a,b))
    print("command方式绑定,不能直接获得event对象")
def mouseTest3(event):
    print("右键单击事件,绑定给所有按钮!!!")
    print(event.widget)

b1 = Button(root, text="测试 bind()绑定")
b1.pack(side="left")
#bind方式绑定事件
b1.bind("<Button-1>",mouseTest1)
#command属性直接绑定事件
b2 = Button(root, text="测试 command2",command=lambda: mouseTest2("gaoqi","xixi"))
b2.pack(side="left")
#给所有Button按钮都绑定右键单击事件<Button-2>
b1.bind_class("Button","<Button-2>",mouseTest3)
root.mainloop()

修改成<Button-3>时单击右键才有反应

# coding=utf-8
# 测试 多种事件绑定方式汇总
from tkinter import *
root = Tk();root.geometry("270x50")
def mouseTest1(event):
    print("bind()方式绑定,可以获取 event 对象")
    print(event.widget)
def mouseTest2(a,b):
    print("a={0},b={1}".format(a,b))
    print("command方式绑定,不能直接获得event对象")
def mouseTest3(event):
    print("右键单击事件,绑定给所有按钮!!!")
    print(event.widget)

b1 = Button(root, text="测试 bind()绑定")
b1.pack(side="left")
#bind方式绑定事件
b1.bind("<Button-1>",mouseTest1)
#command属性直接绑定事件
b2 = Button(root, text="测试 command2",command=lambda: mouseTest2("gaoqi","xixi"))
b2.pack(side="left")
#给所有Button按钮都绑定右键单击事件<Button-3>
b1.bind_class("Button","<Button-3>",mouseTest3)
root.mainloop()

这是我自己电脑问题还是什么?

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2014楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2015楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2016楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2017楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2018楼

from tkinter import *
from tkinter import messagebox
class Application(Frame):

    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.pack()

        self.createWidget()

    def createWidget(self):
        self.label01=Label(self,text="用户名")
        self.label01.pack()
        v1=StringVar()
        self.entry01= Entry(self,textvarible=v1)
        self.entry01.pack()
        v1.set("admin")
        print(v1.get());print(v1.entry01.get())

        self.label02 = Label(self, text="密码")
        self.label02.pack()
        v2 = StringVar()
        self.entry02 = Entry(self, textvarible=v2, show="*")
        self.entry02.pack()
        Button(self, text="登陆", command=self.login).pack()

    def login(self):
        print("用户名"+self.entry01.get())
        print("密码"+self.entry02.get())
        messagebox.showinfo("送花","送你99朵玫瑰花")
        if self.entry01.get()=="gaoqi" and self.entry02.get()=="123456":
            messagebox.showinfo("登陆成功")
        else:
            messagebox.showinfo("废物,登录失败")
if __name__=="__main__":
    root=Tk()
    root.geometry("400x400+200+300")
    root.title("一个经典的Label程序测试")
    app=Application(master=root)
    root.mainloop()

image.png

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 2025楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637