1.self.btn2 = Button(root,image = photo,command = self.login)
使用 image 图片作为按钮,command 作为响应
2.self.btn2.config(state = "disabled")
对按钮进行禁用
config 中 anchor 控制按钮上的图片位置
N NE E SE SW W NW CENTER
默认居中
from tkinter import *
from tkinter import messagebox
class Application(Frame):
‘‘‘GUI程序经典写法‘‘‘
def __init__(self,master = None):
super().__init__(master)
# super() 表示父类的定义,父类使用 master 参数
self.master = master
# 子类定义一个属性接收传递过来的 master 参数
self.pack()
# .pack 设置布局管理器
self.createWidget()
# 在初始化时,将按钮也实现
# master传递给父类 Frame 使用后,子类中再定义一个 master 对象
def createWidget(self):
‘‘‘创建组件‘‘‘
self.btn1 = Button(root,text = ‘登录‘,command = self.login,
width = 5,height = 2,anchor = E)
# command 进行操作的函数
self.btn1.pack()
global photo
photo = PhotoImage(file = "images/登录.gif")
self.btn2 = Button(root,image = photo,command = self.login)
self.btn2.pack()
# self.btn2.config(state = "disabled")
# # 设置按钮为禁用按钮
def login(self):
messagebox.showinfo("博客园","欢迎使用~")
if __name__ == ‘__main__‘:
root = Tk()
# 定义主窗口对象
root.geometry("300x300+400+300")
# 创建大小
root.title("Button 测试")
# 设置标题
app = Application(master = root)
# 传递 master 参数为 主窗口对象
root.mainloop()
2020-04-20
原文:https://www.cnblogs.com/hany-postq473111315/p/12739711.html