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

'''测试一个经典的GUI程序的写法,用面向对象的的方式'''
from tkinter import *

from tkinter import  messagebox
class Appclation(Frame):
    """一个经典的GUI程序的写法"""
    def __init__(self,master=None):
        super().__init__(master)  # super()代表的是父类的定义而不是父类的对象
        self.master=master
        self.pack()
        self.createwidget()
        self.creatwidget2()
    def createwidget(self):
        """  创建lable对象"""
        self.labeo1=Label(self,text='百战程序员',width=10,height=2,bg="black",fg="white")
        self.labeo1.pack()
        self.labeo2=Label(self,text="赵少斯",width=10,height=2,bg='blue',fg="white",font=("黑体",30))
        self.labeo2.pack()
        self.labeo3=Label(self,text='赵少斯好帅\n王杰好丑\n王杰',borderwidth=2,justify="right",relief='solid')
        #创建一个退出按钮
        self.bton1quit=Button(self,text="点击退出",command=root.destroy)
        self.bton1quit.pack() #显示对象
    def creatwidget2(self):
        '''创建组件'''
        self.button2=Button(self)
        self.button2['text']="点击送花"
        self.button2.pack()
        self.button2['command']=self.songhua()
    def songhua(self):
        messagebox.showinfo("你好呀",'给你花')



if __name__ == '__main__':

    root=Tk()
    root.title("一个经典的Lable程序的测试")
    root.geometry("500x800+100+200")
    app=Appclation(master=root)
    root.mainloop()


老师,为啥会同时出现  怎样才能点击组件才能出现第二个


图片.gif

















































Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1881楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 1882楼
Python 全系列/第二阶段:Python 深入与提高/模块 1884楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 1886楼

#coding=utf-8
'''组件对象的绑定
1.通过command属性绑定(适合简单不需获取event对象) Button(root,text="登录",command=login)
2.通过bind()方法绑定(适合需要获取evnet对象) c1 = Canvas(); c1.bind("<Button-1>",drawLine)
   组件类的绑定
调用对象的bind_class函数,将该组件类所有的组件绑定事件:
w.bind_class("Widget","event",eventhanler) 比如:btn01.bind_class("Button","<Button-1>",func)
'''
#多种事件绑定方式汇总
from tkinter import *
root = Tk()
root.geometry("270x30")

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
b1.pack(side="left")
b1.bind("<Button-1>", mouseTest1) #bind方式绑定事件

b2 = Button(root, text="测试command绑定", command=lambda: mouseTest2("xiannv", "zhongnene"))#第二个按钮b2 通过lambda直接传参
b2.pack(side="left")

b1.bind_class("<Button>", "<Button-2>", mouseTest3)#给所有Button按钮都绑定右键单击事件<Button-2>

root.mainloop()

<button-2>为鼠标中键,然而单击鼠标中键没反应

看视频高老师是单击的鼠标右键 我也试了但也没反应

Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1887楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 1888楼
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 1890楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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